JDBC Basics (i) simple use of MySQL

Source: Internet
Author: User

JDBC can do three things: establish a connection to the database, send a statement that operates the database, and process the results.

And the first thing the program does is load the database driver, here I use MySQL:

1 string Drivername=new string ("Com.mysql.jdbc.Driver"); 2 class.forname (drivername);

The database connection object is then obtained, with the parameters of the database URL, user name, and password. Here I use the database named JDBC, the user name is root, the password is 123456:

1 string Url=new string ("Jdbc:mysql://localhost:3306/jdbc"), 2 string User=new string ("root"), 3 string password=new String ("123456"); 4 Connection coon=drivermanager.getconnection (URL, user, password);

Because you want to manipulate the database, get the statement object:

1 Statement Statement = Connection.createstatement ();

The Execute (String sql) method encapsulated within the statement object and the ExecuteQuery (String sql) method and the Executeupdate (String sql) method can be used to execute the SQL statement. This allows you to work with the database.

1   String sql = null, 2   ResultSet Resultse = null, 3  4   //Create Student table 5   sql= "CREATE TABLE Student (ID cha R (9) Primary Key,name char (9) unique) "; 6   statement.execute (SQL);         7           8   //Add tuple 9   sql = "INSERT into Student (id,name) VALUES (' 0001 ', ' Zhangsan ')";   Statement.executeupdate (SQL), one   page//Query Student table   sql= "SELECT * from Student";   ResultSet = Statement.executequery (SQL);             resultset.next (   ) {       System.out.println ("Name:" + Resultset.getstring ("name"));       System.out.println ("ID:" +resultset.getstring ("id"));   }20  21   //delete tuple   sql= "Delete from Student where id= ' 0001 '";   statement.executeupdate (SQL);  25   //delete table Student26   sql= "drop table Teacher";   statement.execute (SQL);    

Close the resource after you have finished working on the database, sequentially resultset,statement,connection:

1 try {2     if (resultSet! = null) 3         resultset.close (); 4  } catch (SQLException e) {5        e.printstacktrace (); 6  } finally {7        resultSet = null; 8        try {9            if (statement! = NULL)              statement.close ();        (SQLException e) {              e.printstacktrace ();        finally {              statement = null;15              try {+                  if (connection! = NULL)                     connection.close ();              catch (SQLException e) {                     e.printstacktrace ()              } finally {                     Connection = null;22              }23         }24  }

Close () throws an exception and requires a Try/catch statement block. To ensure the release of a resource, the call to the close () method is placed in the finally statement block, and the object is judged null before releasing the resource. At this point, using the JDBC Connection database for a simple operation is complete.

JDBC Basics (i) simple use of MySQL

Related Article

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.