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.jar
Add 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.