1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importjava.sql.Statement;6 ImportJava.util.ResourceBundle;7 8 Public classConnnectionutils {9 Ten Private StaticString dbdriver = "";//Com.mysql.jdbc.Driver One Private StaticString url = "";//jdbc\:mysql\://Localhost\:3306/dabasename?useunicode\=true&characterencoding\=utf-8 A Private StaticString user = "";//username - Private StaticString PASSW = "";//Password - the StaticConnection conn=NULL; - - Static { - +ResourceBundle Source = resourcebundle.getbundle ("config");//config.properties -Dbdriver = source.getstring ("Driverclassname"); +url = source.getstring ("Jdbc_url"); Auser = Source.getstring ("Jdbc_username"); atPASSW = source.getstring ("Jdbc_password"); - } - - Public StaticConnection getconnection () { - Try { - //Load Driver in Class.forName (dbdriver); - //Get links toconn =drivermanager.getconnection (URL,USER,PASSW); +}Catch(Exception e) { - e.printstacktrace (); the } * $ returnConn;Panax Notoginseng } - the //close link after use + Public Static voidClose () { A if(Conn! =NULL) { the Try { + conn.close (); -}Catch(SQLException e) { $ e.printstacktrace (); $ } - } - } the - Wuyi the - Public Static voidMain (string[] args) { WuConnection conn =connnectionutils.getconnection (); - SYSTEM.OUT.PRINTLN (conn); AboutString sql = "SELECT * FROM TableName"; $ - Try { - //Execute SQL -Statement Statement =conn.createstatement (); A //get the result set +ResultSet result =statement.executequery (SQL); the - //If there is a result set, the result is processed $ while(Result.next ()) { theString App_url = result.getstring ("Onestringcolumnname"); the System.out.println (app_url); the the } - //Close Link in connnectionutils.close (); the}Catch(SQLException e) { the e.printstacktrace (); About } the the } the}
qq:871820604
Java link jdbc