JDBC Operation steps

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.