一、JDBC常用类和接口
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API。JDBC是Java访问数据库的标准规范,可以为不同的关系型数据库提供统一访问,它由一组用Java语言编写的接口和类组成。
JDBC**与数据库驱动的关系:接口与实现类的关系。**
二、JDBC常用类和接口
JDBC有关的类:都在java.sql 和 javax.sql 包下.
接口在Java中是用来定义 行为规范的
. 接口必须有实现类.
JDBC规范(四个核心对象):
DriverManager:用于注册驱动
Connection: 表示与数据库创建的连接
Statement: 操作数据库sql语句的对象
ResultSet: 结果集或一张虚拟表
特别注意:
Mysql驱动的driverClass 和url 的写法
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/text
1.JDBC 初体验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Test public void demo01() throws SQLException { DriverManager.registerDriver(new Driver()); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "111"); String sql = "select * from user;"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " : " + username + " : " + password + " : " + email); } rs.close(); stmt.close(); conn.close(); }
|
2.JDBC工具类的提取()
方式一、
–src下放连接数据库的配置文件
|--properties
1 2 3 4 5 6 7
|
#mysql driverClass=com.mysql.jdbc.Driver url=jdbc:mysql: user=root password=111
|
–util包
|--JDBCUtils 类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| public class JDBCUtils { private static String driverClass; private static String url; private static String username; private static String password; static { Properties prop = new Properties(); try { prop.load(new FileReader("jdbc.properties")); driverClass = prop.getProperty("driverClass"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); loadDriver(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("文件资源加载失败!"); } } public static void loadDriver() { try { Class.forName(driverClass); } catch (ClassNotFoundException e) { throw new RuntimeException("驱动加载失败!"); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } public static void release(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } release(conn, stmt); } public static void release(Connection conn, Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } }
|
3.测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| @Test public void test_update() { Connection conn = null; Statement stmt = null; try { conn = JDBCUtils.getConnection(); String sql = "update user set username = 'zhaoliu', password = '123', email = 'zhaoliu@youjian.cn' where id = 4;"; stmt = conn.createStatement(); int affectedRowNum = stmt.executeUpdate(sql); System.out.println(affectedRowNum); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, stmt); } } @Test public void test_delete() { Connection conn = null; Statement stmt = null; try { conn = JDBCUtils.getConnection(); String sql = "delete from user where id = 5;"; stmt = conn.createStatement(); int affectedRowNum = stmt.executeUpdate(sql); System.out.println(affectedRowNum); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, stmt); } } @Test public void test_insert() { Connection conn = null; Statement stmt = null; try { conn = JDBCUtils.getConnection(); String sql = "insert into user values(null, 'xiaoqi', '123', 'xiaoqi@youjian.cn');"; stmt = conn.createStatement(); int affectedRowNumber = stmt.executeUpdate(sql); System.out.println(affectedRowNumber); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, stmt); } }
Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select * from user where username = ? and password = ?;"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password); rs = stmt.executeQuery(); if (rs.next()) { int id = rs.getInt("id"); String u_name = rs.getString("username"); String u_pwd = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " : " + u_name + " : " + u_pwd + " : " + email); System.out.println("登录成功!"); } else { System.out.println("登录失败! 用户名或密码错误!"); } } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.release(conn, stmt, rs); } } }
|