To connect to a database using JDBC in Java

Source: Internet
Author: User
Tags stmt

What are the steps to use JDBC to connect to a database in Java?Adoption Rate:46% Class 6 2015.06.30 1. Registration Driver
Class.forName ("Com.mysql.jdbc.Driver");//This is the driver that connects the MySQL database
2. Get a database connection
Java.sql.Connection conn=java.sql.drivermanager.getconnection (); 3. Get an expression
Java.sql.Statement stmt=conn.createstatement ("jdbc:mysql://localhost/test?useunicode=true&characterencoding =gbk "," root "," null ");//Three parameters are the URL of the database connection, user name, password 4. Execute SQL
Java.sql.ResultSet rs=stmt.executequery ("SELECT * from user"); 5. Display the data in the result set
while (Rs.next ()) {
System.out.println (Rs.getint (1));
System.out.println (rs.getstring ("username"));
System.out.println (rs.getstring ("password"));
System.out.pringln ();
}//executing an INSERT statement
Stmt.executeupdate ("INSERT into user values (1, ' Chinese ', ' 345 ')");
6. Releasing Resources
Rs.close ();
Stmt.close ();
Conn.close ();

To connect to a database using JDBC in Java

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.