Java JDBC Connection

Source: Internet
Author: User

1.java connect MySQL database: First need to load MySQL driver,: https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.40.zip

2. Place the downloaded jar driver under the project's Lib folder, and then bulid in path.

3. Register the JDBC Driver:

Class.forName ("Com.mysql.jdbc.Driver");

4. Create a database connection for the connection object:

// replace your own database address Private Static final url= "Jdbc:mysql://localhost:3306/book"// database user name privatestatic final user= "root"// database password privatestaticfinal Password= "123456"Connection conn=drivermanager.getconnection (Url,user,password)
You need to import Java.sql.connection,java.sql.drivermanager instead of java.mysql.Connection here.
  

5. Perform the operation with the statement object:

Statement stmt =conn.createstatement ();//Get query Results ResultSet rs=stmt.executequery (SQL);

6. Close the database resource:

Rs.close (); Stmt.close (); Conn.close ();

  

 

Java JDBC Connection

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.