1. First to load the Java MySQL driver, add the downloaded Mysql-connect-bin-java.jar package to the project
2. Then through class.forname ("Com.mysql.jdbc.Driver"); Load MySQL data-driven
3. Through the getconnection URL mode to connect the attention, sometimes if the report
Connection Pooling Connection Database report communications link failure error, change your localhost to 127.0.0.1 try
1 Packagechp07;2 ImportJava.sql.*;3 4 Importcom.mysql.jdbc.PreparedStatement;5 Public classJdbc_test {6 StaticPreparedStatement PS =NULL;7 StaticResultSet rs;8 Public Static voidMain (string[] args) {9 //TODO auto-generated Method StubTenConnection con =NULL;//Create a Connection object to connect to the database One Try { AClass.forName ("Com.mysql.jdbc.Driver");//load MySQL data-driven - -Con =Drivermanager.getconnection ( the"Jdbc:mysql://127.0.0.1:3306/mysql", "root", "123456");//Create a data connection - //System.out.println ("database connection succeeded"); - -String sql = "Select User from ' User ' U";//SQL statement to delete data +PS =(PreparedStatement) con.preparestatement (SQL); -rs =ps.executequery (); + while(Rs.next ()) { ASystem.out.println (rs.getstring ("User")); at - } -Con.close ();//To close a database connection -}Catch(Exception e) { -SYSTEM.OUT.PRINTLN ("Database connection failed" +e.getmessage ()); - } in - } to +}