Connect Java to MySQL and javamysql

Source: Internet
Author: User

Connect Java to MySQL and javamysql

1. Download and install Connector/J at http://www.mysql.com/products/connector /. Connector/J is a JDBC driver package specially developed for MySQL.
2.mysql-connector-java-5.1.36-bin.jarAdd the classpath to the environment variable or the Java Build Path to the project.
3. JDBC basic programming steps:

  • Load driver
    The following methods are used:Class.forName()Or
    Class.forName().newInstance()Or
    new DriverName()
  • Connect to database
    DriverManager.getConnection()
  • Execute SQL statements
    Connection.CreateStatement()
    Statement.executeQuery()
    Statement.executeUpdate()
  • Get result set
    while(rs.next())
  • Show data
    Convert various types in the database to types in java (getXXX) Method
  • Close
    close the resultset
    close the statement
    close the connection

Instance:

Package MS; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. statement; import java. SQL. resultSet; public class TestMySQL {public static void main (String [] args) {ResultSet rs = null; Statement stmt = null; Connection conn = null; try {Class. forName ("com. mysql. jdbc. driver "); // create an instance String url of the class identified by this String =" jdbc: mysql: // localhost: 3306/test "; // identify a registered Driver String user = "root"; String pwd = ""; conn = DriverManager. getConnection (url, user, pwd); stmt = conn. createStatement (); String query = "select * from person where age> 18"; rs = stmt.exe cuteQuery (query); while (rs. next () {String id = rs. getString ("id"); String name = rs. getString (2); int age = rs. getInt ("age"); System. out. println (id + "\ t" + name + "\ t" + age) ;}} catch (ClassNotFoundEx Ception e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace () ;}finally {try {if (rs! = Null) {rs. close () ;}if (stmt! = Null) {stmt. close ();} if (conn! = Null) {conn. close () ;}} catch (SQLException e) {e. printStackTrace ();}}}}

PreparedStatement preprocessing statement:

String sql = "insert into person values(?,?,?)";PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setString(1, "005");pstmt.setString(2, "Zhao");pstmt.setInt(3, 18);pstmt.executeUpdate();

Batch Processing of statement statements:

Statement stmt = conn.createStatement();stmt.addBatch("insert into person values('006', 'Zeng', 26)");stmt.addBatch("insert into person values('007', 'Liu', 24)");stmt.addBatch("insert into person values('008', 'Zeng', 17)");stmt.executeBatch();

Batch Processing of PreparedStatement statements

String sql = "insert into person values(?,?,?)";PreparedStatement pstmt = conn.prepareStatement(sql);pstmt.setString(1, "006");pstmt.setString(2, "Zeng");pstmt.setInt(3, 26);pstmt.setString(1, "007");pstmt.setString(2, "Liu");pstmt.setInt(3, 24);pstmt.setString(1, "008");pstmt.setString(2, "Zeng");pstmt.setInt(3, 17);pstmt.executeUpdate();

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.