The driver is a JDBC driver.
The URL is the address of the database.
Usrname and password are database user names and passwords, respectively.
The connection class is used to connect to MySQL.
The resultset is used to store the result, which is typically used with the SELECT statement.
Import Java.sql.*;class MySQL {Private StaticFinal String Driver ="Com.mysql.jdbc.Driver";Private StaticFinal String URL ="Jdbc:mysql://localhost:3306/school";Private StaticFinal String Usrname ="Root";Private StaticFinal String Password ="";PrivateConnection con =NULL;PrivatePreparedStatement PS =NULL;PrivateResultSet rs =NULL;//Create student table Public MySQL() {String sql ="CREATE table if not EXISTS student (name Char),"+"Sno Char (Ten) primary key, age smallint, sex char (6),"+"Sdept char (4))";Try{Class.forName (driver); }Catch(ClassNotFoundException e) {//throws an exception with no driver foundE.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password);//Connect to databasePS = con.preparestatement (SQL);//Precompiled SQL statementsPs.executeupdate ();//Update //Close the corresponding object, note the order problem, irreversible. Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e) {E.printstacktrace (); } }Catch(SQLException e) {E.printstacktrace (); } }//Insert a message Public void Addinfo(string name, String sno,intAge, String sex, String sdept) {String sql ="INSERT into student (name, Sno, age, Sex, sdept)"+"VALUES ('"+ name +"', '"+ Sno +"',"+ Age +", '"+ Sex +"', '"+ Sdept +"');";Try{Class.forName (driver); }Catch(ClassNotFoundException e) {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password); PS = con.preparestatement (SQL); Ps.executeupdate ();Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e) {E.printstacktrace (); } }Catch(SQLException e) {E.printstacktrace (); } }//Delete a message Public void Deleteinfo(String name) {String sql ="Delete from student where name = '"+ name +"';";Try{Class.forName (driver); }Catch(ClassNotFoundException e) {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password); PS = con.preparestatement (SQL); Ps.executeupdate ();Try{if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e) {E.printstacktrace (); } }Catch(SQLException e) {E.printstacktrace (); } }//Query a piece of information Public void SearchInfo(String name) {String sql ="SELECT * from student where name is like '"+ name +"';";Try{Class.forName (driver); }Catch(ClassNotFoundException e) {E.printstacktrace (); }Try{con = drivermanager.getconnection (URL, usrname, password); PS = con.preparestatement (SQL); rs = Ps.executequery ();//Accept the results of the query and store them in Rs. if(Rs.next ()) { Do{System. out. println ("Name:"+ rs.getstring (1) +", Sno:"+ rs.getstring (2) +", Age:"+ rs.getstring (3) +", Sex:"+ rs.getstring (4) +", sdept:"+ rs.getstring (5)); } while(Rs.next ()); }Else{//If not presentSystem. out. println ("There is no one named"+ name +"."); }Try{if(rs! =NULL) Rs.close ();if(PS! =NULL) Ps.close ();if(Con! =NULL) Con.close (); }Catch(SQLException e) {E.printstacktrace (); } }Catch(SQLException e) {E.printstacktrace (); } }} Public classTESTJDBC { Public Static void Main(string[] args) {System. out. println ("Creating table student!"); MySQL sq =NewMySQL (); System. out. println ("Testing Add data!"); Sq.addinfo ("Mike.","2013210888", A,"Male","CS"); System. out. println ("Testing Delete data!"); Sq.deleteinfo ("Mike."); System. out. println ("Testing Search data!"); Sq.searchinfo ("Mike."); System. out. println ("success!"); }}
Connect MySQL with JDBC and use MySQL