1, set up the table user and insert data (at this time the database is users, the user name is root, the password is 123456)
Create Table User varchar( varchar); Insert into User Values ('tufujie','123456');
2. Writing Java files
//demonstrates how to connect to MySQL using the COM.MYSQL.JDBC connection Bridge Packagecom.tfj.db;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement; Public classJavactmysql { Public Static voidMain (string[] args) {Try { //1. Load (load) driverClass.forName ("Com.mysql.jdbc.Driver"); //2. Get the link (registration) /** Connection ct = drivermanager.getconnection (* "jdbc:mysql://localhost:3306/users? Useunicode=true&characterencoding=utf-8 ", *"root","123456"); */ //or you can do it the following way.Connection ct =DriverManager. getconnection ("jdbc:mysql://localhost:3306/Users? user=Root&password=123456&useunicode=true&characterenconding=utf-8 "); //3. Identical to SQL Server, SQL statements if(ct! =NULL&&!ct.isclosed ()) System.out.println ("Database Test Connection succeeded"); Statement SM=ct.createstatement (); ResultSet RS= Sm.executequery ("SELECT * FROMUser"); while(Rs.next ()) {//User nameSYSTEM.OUT.PRINTLN ("Administrator name:" + rs.getstring (1) + "Password:" + rs.getstring (2)); } //close an open resourceRs.close (); Sm.close (); Ct.close (); } Catch(Exception e) {e.printstacktrace (); } }}
Java Connection MySQL Database