java连接mysql数据库都有固定的操作,步骤如下:
- 加载mysql驱动,一般都是com.mysql.jdbc.Driver。
- 提供JDBC连接的URL。
-
- 创建数据库的连接。要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象。
- 创建一个Statement。
- 要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3种类型:
- 1)执行静态SQL语句。通常通过Statement实例实现。
- 2)执行动态SQL语句。通常通过PreparedStatement实例实现(常用)。
- 3)执行数据库存储过程。通常通过CallableStatement实例实现。
-
- 具体实现方式:
- Statement stmt = con.createStatement() ;
- PreparedStatement pstmt = con.prepareStatement(sql) ;
- CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;
-
- 执行SQL语句
- Statement接口提供了三种执行SQL语句的方法:executeQuery 、executeUpdate和execute 。
- 1)ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句,返回一个结果集(ResultSet)对象。
- 2)int executeUpdate(String sqlString):用于执行INSERT、UPDATE或DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等
- 3)execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的语句。
- 具体实现方式:
- ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
- int rows = stmt.executeUpdate("INSERT INTO ...") ;
- boolean flag = stmt.execute(String sql) ;
-
- 处理结果
- 有两种情况:
- 1)执行更新返回的是本次操作影响到的记录数。
- 2)执行查询返回的结果是一个ResultSet对象。
- • ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些行中数据的访问。
- • 使用结果集(ResultSet)对象的访问方法获取数据:
- while(rs.next()){
- String name = rs.getString("name") ;
- String psw = rs.getString(1) ; // 此方法比较高效
- }
- (列是从左到右编号的,并且从列1开始)
-
- 关闭JDBC对象
- 操作完成以后要把所有使用的JDBC对象全都关闭,以释放JDBC资源,关闭顺序和声明顺序相反:
- 1)关闭记录集
- 2)关闭声明
- 3)关闭连接对象
-
if(rs != null){ // 关闭记录集 try{ rs.close() ; }catch(SQLException e){ e.printStackTrace() ; } } if(stmt != null){ // 关闭声明 try{ stmt.close() ; }catch(SQLException e){ e.printStackTrace() ; } } if(conn != null){ // 关闭连接对象 try{ conn.close() ; }catch(SQLException e){ e.printStackTrace() ; } }
-
请注意,如果仅仅是创建一个数据库连接的话,步骤4、5、6不是必要的。所以,如果你是初学者,只需要用前面的步骤测试数据库是否连接成功即可,不要做过多的操作,那样你就能发现其实JDBC连接数据库的方法其实并不难。但是,当你操作完数据库,请你记住关闭左右连接对象,释放资源,这不仅是个好习惯,还能在你处理较大的数据量是避免不必要的麻烦。
下面给出一个例子,从mysql中取出数据并存到二维数组里,数据用的是机器学习中的鸢尾花iris数据(在机器学习数据集获取官方网站UCI中点击打开链接),代码如下:
DBConnection.java
package db;import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;/*** * 数据库连接类,通用* * @author lsy* */ public class DBConnection {public static final String driver = "com.mysql.jdbc.Driver";// 驱动public static final String url = "jdbc:mysql://localhost:3306/mydb";// mysql固定的URL:jdbc:mysql://localhost:3306/数据库名(我这里是mydb)public static final String user = "root";// 我的数据库的用户名public static final String pwd = "123";// 我的数据库密码public static Connection dBConnection() {Connection con = null;try {// 加载mysql驱动器Class.forName(driver);// 建立数据库连接con = DriverManager.getConnection(url, user, pwd);} catch (ClassNotFoundException e) {System.out.println("加载驱动器失败");e.printStackTrace();} catch (SQLException e) {System.out.println("注册驱动器失败");e.printStackTrace();}return con;} }
SelectData.java
package dao;import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;import db.DBConnection;/*** * 取出数据,存到二维数组里* * @return flowers* @author lsy*/ public class SelectData {public static final String SELECT = "select* from iris_PCA";public static final int k = 4;// 4个属性public double[][] getFlowers() throws SQLException {Connection con = DBConnection.dBConnection();ResultSet rs;// 创建结果集PreparedStatement pstmt = con.prepareStatement(SELECT);// 创建一个PreparedStatement对象rs = pstmt.executeQuery();// 为初始化数组的大小提供方便int sample = 0;while (rs.next()) {sample++;}double[][] flower = new double[sample][k];rs = pstmt.executeQuery();// 特别重要,否则取到的全是0。因为执行上面的while(rs.next())后,ResultSet对象的下标已指到0。// API:当生成ResultSet对象的Statement对象关闭、重新执行或用来从多个结果的序列获取下一个结果时,ResultSet对象将自动关闭。for (int i = 0; rs.next(); i++) {for (int j = 0; j < k; j++) {flower[i][j] = rs.getDouble(j + 2);}}// 输出二维数组System.out.println("花花: ");for (int i = 0; i < flower.length; i++) {for (int j = 0; j < flower[0].length; j++) {System.out.print(flower[i][j] + "\t");}System.out.println();}pstmt.close();rs.close();con.close();return flower;}public static void main(String[] args) {try {SelectData selectData = new SelectData();selectData.getFlowers();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }
输出结果:
花花: 5.1 3.5 1.4 0.2 4.9 3.0 1.4 0.2 4.7 3.2 1.3 0.2 4.6 3.1 1.5 0.2 5.0 3.6 1.4 0.2 5.4 3.9 1.7 0.4 4.6 3.4 1.4 0.3 5.0 3.4 1.5 0.2 4.4 2.9 1.4 0.2 4.9 3.1 1.5 0.1 5.4 3.7 1.5 0.2 4.8 3.4 1.6 0.2 4.8 3.0 1.4 0.1 4.3 3.0 1.1 0.1 5.8 4.0 1.2 0.2 5.7 4.4 1.5 0.4 5.4 3.9 1.3 0.4 5.1 3.5 1.4 0.3 5.7 3.8 1.7 0.3 5.1 3.8 1.5 0.3 5.4 3.4 1.7 0.2 5.1 3.7 1.5 0.4 4.6 3.6 1.0 0.2 5.1 3.3 1.7 0.5 4.8 3.4 1.9 0.2 5.0 3.0 1.6 0.2 5.0 3.4 1.6 0.4 5.2 3.5 1.5 0.2 5.2 3.4 1.4 0.2 4.7 3.2 1.6 0.2 4.8 3.1 1.6 0.2 5.4 3.4 1.5 0.4 5.2 4.1 1.5 0.1 5.5 4.2 1.4 0.2 4.9 3.1 1.5 0.1 5.0 3.2 1.2 0.2 5.5 3.5 1.3 0.2 4.9 3.1 1.5 0.1 4.4 3.0 1.3 0.2 5.1 3.4 1.5 0.2 5.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