Import jar Package: Mysql.connector-java-5.0.8-bin.jar
String Driver = "Com.mysql.jdbc.Driver"; String URL= "Jdbc:mysql://127.0.0.1:3306/ali"; Connection Conn=NULL; Try{class.forname (driver); Conn= Drivermanager.getconnection (URL, "root", "" "); //SYSTEM.OUT.PRINTLN (conn);}Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }
ResultSet rs =NULL;PreparedStatement PS=NULL; String SQL= "SELECT * from Think_user;"; Try{PS=conn.preparestatement (SQL); RS=Ps.executequery (); while(Rs.next ()) {System.out.println (rs.getstring ("id")); System.out.println (Rs.getstring ("Username")); System.out.println (Rs.getstring ("Turename")); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }
Finally, you must remember to close the link Oh!!
The simplest Java-driven JDBC link MySQL database