Packagecom.itheima.test;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importorg.junit.Test;ImportCom.mysql.jdbc.Driver; Public classJdbcTest01 {/**drop Database day15; Create Database day15; Use DAY15; CREATE TABLE t1 (ID int primary KEY, content varchar (100)); INSERT into T1 values (3, ' Test JDBC3 '); */@Test Public voidTestadd () {Try { //1. The registration drive Com.mysql.jdbc.Driver represents the Java.sql.Driver implementation class which is placed in the MySQL drive jar fileDrivermanager.registerdriver (NewDriver ()); //2. Get the connection URL to the database: connection string representing the connection database (MySQL document 26.3.3) User: username password: passwordConnection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day15", "root", "root"); //3. Get the object used to send and execute SQL statements StatementStatement st =con.createstatement (); //4. Execute the statementSt.executeupdate ("INSERT into T1 values (1, ' Test jdbc ')");//Insert Update Delete//st.executeupdate ("Update T1 set content= ' test JDBC Update ' where id=1");//Insert Update Delete//st.executeupdate ("delete from T1 where id=1");//Insert Update Delete//5. If the query operation, there is a result set, it is necessary to deal with it//6. Closing resources in order (Result set-----Statement-------Connection)St.close (); Con.close (); } Catch(Exception e) {e.printstacktrace (); } } //Enquiry@Test Public voidTestquery () {Try { //1. The registration drive Com.mysql.jdbc.Driver represents the Java.sql.Driver implementation class which is placed in the MySQL drive jar fileDrivermanager.registerdriver (NewDriver ()); //2. Get the connection URL to the database: connection string representing the connection database (MySQL document 26.3.3) User: username password: passwordConnection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day15", "root", "root"); //3. Get the object used to send and execute SQL statements StatementStatement st =con.createstatement (); //4. Execute the statementResultSet rs = st.executequery ("Select id,content from T1"); //5. If the query operation, there is a result set, it is necessary to deal with it while(Rs.next ()) {//Rs.next () represents the next line GetObject (1) represents the value of the first column in the first row + the value of the second column in the first row System.out.println (Rs.getobject (1) + "," +rs.getobject (2)); } //6. Closing resources in order (Result set-----Statement-------Connection)St.close (); Con.close (); } Catch(Exception e) {e.printstacktrace (); } }}
JDBC Operation steps