Java-JDBC
1、导入jar包
2、步骤
//1、加载驱动
//2、获取连接通道 指定要连接的数据库
//3、构建sql语句
//4、执行sql语句
//5、关闭资源

1 package com.keke; 2 3 import org.junit.Test; 4 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.Statement; 8 9 public class Test01 { 10 /** 11 * 第一个JDBC测试案例:往student表插入1条数据 12 * @param args 13 */ 14 public static void main(String[] args) { 15 16 } 17 18 @Test 19 public void insertIntoStudent() throws Exception{ 20 //1、加载驱动 21 Class.forName("com.mysql.cj.jdbc.Driver"); 22 //2、获取连接通道 指定要连接的数据库 23 Connection connection= DriverManager.getConnection( 24 "jdbc:mysql://localhost:3306/zhangmingming", 25 "root", 26 "root"); 27 //3、构建sql语句 28 String sql_insert="insert into student (id,name,age)values(4,'keke',24)"; 29 //4、执行sql语句 30 Statement statement = connection.createStatement(); 31 int i = statement.executeUpdate(sql_insert); 32 System.out.println("i = " + i); 33 //5、关闭资源 34 statement.close(); 35 connection.close(); 36 } 37 }
执行结果:
备注:
3、数据库连接方式
3.1 数据库URL
首先就是在上面配置需要连接的数据库的URL,不同的数据库的连接方式是不一样的。我们分别介绍下MySQL,Oracle和SQLServer的连接方式
MySQL连接的URL的方式
-
jdbc:mysql://主机名称:mysql服务端口号/数据库名称?参数=值&参数=值
-
jdbc:mysql://localhost:3306/bobo
-
jdbc:mysql://localhost:3306/bobo?useUnicode=true&characterEncoding=utf8(如果JDBC程序与服务器端的字符集不一致,会导致乱码,那么可以通过参数指定服务器端的字符集)
-
jdbc:mysql://localhost:3306/bobo?user=root&password=123456
Oracle 9i的连接URL编写方式:
-
jdbc:oracle:thin:@主机名称:oracle服务端口号:数据库名称
-
jdbc:oracle:thin:@localhost:1521:bobo
SQLServer的连接URL编写方式:
-
jdbc:sqlserver://主机名称:sqlserver服务端口号:DatabaseName=数据库名称
-
jdbc:sqlserver://localhost:1433:DatabaseName=bobo

1 /** 2 * 获取connection对象的方式 3 * 第一种方式:通过driver直接获取 4 */ 5 @Test 6 public void getConnection1() throws Exception{ 7 Driver driver= new Driver(); 8 //获取string类型的url 9 String url="jdbc:mysql://localhost:3306/zhangmingming"; 10 //获取Properties对象 11 Properties info = new Properties(); 12 info.setProperty("user","root"); 13 info.setProperty("password","root"); 14 //通过driver对象获取connection对象 15 Connection connect = driver.connect(url, info); 16 System.out.println("connect = " + connect); 17 }
方式2:通过反射获取driver,然后直接获取

1 /** 2 * 获取connection对象的方式 3 * 第二种方式:通过反射获取driver,然后直接获取 4 */ 5 @Test 6 public void getConnection2() throws Exception{ 7 //1.实例化Driver 8 String className = "com.mysql.cj.jdbc.Driver"; 9 Class clazz = Class.forName(className); 10 Driver driver = (Driver) clazz.newInstance(); 11 //2.获取string类型的url 12 String url="jdbc:mysql://localhost:3306/zhangmingming"; 13 //获取Properties对象 14 Properties info = new Properties(); 15 info.setProperty("user","root"); 16 info.setProperty("password","root"); 17 //3.通过driver对象获取connection对象 18 Connection connect = driver.connect(url, info); 19 System.out.println("connect = " + connect); 20 }
方式3:通过DriverManager获取

/** * 获取connection对象的方式 * 第三种方式:通过DriverManager获取 */ @Test public void getConnection3() throws Exception{ //1.数据库连接的4个基本要素: String url = "jdbc:mysql://localhost:3306/zhangmingming"; String user = "root"; String password = "root"; String driverName = "com.mysql.cj.jdbc.Driver"; //2.实例化Driver Class clazz = Class.forName(driverName); Driver driver = (Driver) clazz.newInstance(); //3.注册驱动 DriverManager.registerDriver(driver); //4.获取连接 Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); }
方式4:通过Class.forname把这个驱动加载到内存中,创建了对应的类对象

1 /** 2 * 获取connection对象的方式 3 * 第四种方式:通过Class.forname把这个驱动加载到内存中,创建了对应的类对象 4 */ 5 @Test 6 public void getConnection4() throws Exception{ 7 Class.forName("com.mysql.cj.jdbc.Driver"); 8 String url = "jdbc:mysql://localhost:3306/zhangmingming"; 9 String user = "root"; 10 String password = "root"; 11 Connection connection = DriverManager.getConnection(url, user, password); 12 System.out.println("connection = " + connection); 13 }
方式5:把数据库相关的核心信息写到一个独立的配置文件中,通过Property来加载

1 /** 2 * 获取connection对象的方式 3 * 第五种方式:把数据库相关的核心信息写到一个独立的配置文件中 4 * 通过Property来加载 5 */ 6 @Test 7 public void getConnection5() throws Exception{ 8 //1.加载配置文件 9 InputStream resourceAsStream = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 10 Properties pros = new Properties(); 11 pros.load(resourceAsStream); 12 Class.forName("com.mysql.cj.jdbc.Driver"); 13 //2.通过properties对象获取配置信息 14 String url = pros.getProperty("url"); 15 String user = pros.getProperty("user"); 16 String password = "root";pros.getProperty("password"); 17 Connection connection = DriverManager.getConnection(url, user, password); 18 System.out.println("connection = " + connection); 19 }
4、CRUD操作

1 package com.keke; 2 3 import org.junit.Test; 4 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.sql.*; 8 import java.util.Properties; 9 10 public class Test02 { 11 /** 12 * 学生表的CRUD操作 13 * 添加学生信息 14 */ 15 @Test 16 public void insert(){ 17 // 1.加载配置文件 18 InputStream in = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 19 Properties properties = new Properties(); 20 21 try { 22 // 2.从Properties对象中获取对应的配置信息 23 properties.load(in); 24 Class.forName(properties.getProperty("driverClass")); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 catch (ClassNotFoundException e) { 29 e.printStackTrace(); 30 } 31 String url=properties.getProperty("url"); 32 String user=properties.getProperty("user"); 33 String password=properties.getProperty("password"); 34 // 获取Connection的连接对象 35 Connection connection = null; 36 Statement statement=null; 37 try { 38 connection = DriverManager.getConnection(url, user, password); 39 String sql_insert="insert into student (id,name,age)values(5,'shanshan',21)"; 40 // 获取Statement对象 41 statement = connection.createStatement(); 42 // 执行SQL语句 43 int i = statement.executeUpdate(sql_insert); 44 System.out.println("i = " + i); 45 46 statement.close(); 47 } catch (SQLException throwables) { 48 throwables.printStackTrace(); 49 }finally { 50 if(connection!=null){ 51 try { 52 connection.close(); 53 } catch (SQLException throwables) { 54 throwables.printStackTrace(); 55 } 56 } 57 if(statement!=null){ 58 try { 59 statement.close(); 60 } catch (SQLException throwables) { 61 throwables.printStackTrace(); 62 } 63 } 64 } 65 66 } 67 68 /** 69 * 学生表的CRUD操作 70 * 更新学生信息 71 */ 72 @Test 73 public void update(){ 74 // 1.加载配置文件 75 InputStream in = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 76 Properties properties = new Properties(); 77 78 try { 79 // 2.从Properties对象中获取对应的配置信息 80 properties.load(in); 81 Class.forName(properties.getProperty("driverClass")); 82 } catch (IOException e) { 83 e.printStackTrace(); 84 } 85 catch (ClassNotFoundException e) { 86 e.printStackTrace(); 87 } 88 String url=properties.getProperty("url"); 89 String user=properties.getProperty("user"); 90 String password=properties.getProperty("password"); 91 // 获取Connection的连接对象 92 Connection connection = null; 93 Statement statement=null; 94 try { 95 connection = DriverManager.getConnection(url, user, password); 96 String sql_insert="update student set name='chuanzhang' where id=4"; 97 // 获取Statement对象 98 statement = connection.createStatement(); 99 // 执行SQL语句 100 int i = statement.executeUpdate(sql_insert); 101 System.out.println("i = " + i); 102 103 statement.close(); 104 } catch (SQLException throwables) { 105 throwables.printStackTrace(); 106 }finally { 107 if(connection!=null){ 108 try { 109 connection.close(); 110 } catch (SQLException throwables) { 111 throwables.printStackTrace(); 112 } 113 } 114 if(statement!=null){ 115 try { 116 statement.close(); 117 } catch (SQLException throwables) { 118 throwables.printStackTrace(); 119 } 120 } 121 } 122 123 } 124 125 /** 126 * 学生表的CRUD操作 127 * 删除学生信息 128 */ 129 @Test 130 public void delete(){ 131 // 1.加载配置文件 132 InputStream in = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 133 Properties properties = new Properties(); 134 135 try { 136 // 2.从Properties对象中获取对应的配置信息 137 properties.load(in); 138 Class.forName(properties.getProperty("driverClass")); 139 } catch (IOException e) { 140 e.printStackTrace(); 141 } 142 catch (ClassNotFoundException e) { 143 e.printStackTrace(); 144 } 145 String url=properties.getProperty("url"); 146 String user=properties.getProperty("user"); 147 String password=properties.getProperty("password"); 148 // 获取Connection的连接对象 149 Connection connection = null; 150 Statement statement=null; 151 try { 152 connection = DriverManager.getConnection(url, user, password); 153 String sql_insert="delete from student where id=5"; 154 // 获取Statement对象 155 statement = connection.createStatement(); 156 // 执行SQL语句 157 int i = statement.executeUpdate(sql_insert); 158 System.out.println("i = " + i); 159 160 statement.close(); 161 } catch (SQLException throwables) { 162 throwables.printStackTrace(); 163 }finally { 164 if(connection!=null){ 165 try { 166 connection.close(); 167 } catch (SQLException throwables) { 168 throwables.printStackTrace(); 169 } 170 } 171 if(statement!=null){ 172 try { 173 statement.close(); 174 } catch (SQLException throwables) { 175 throwables.printStackTrace(); 176 } 177 } 178 } 179 180 } 181 182 /** 183 * 学生表的CRUD操作 184 * 查询学生信息 185 */ 186 @Test 187 public void select(){ 188 // 1.加载配置文件 189 InputStream in = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 190 Properties properties = new Properties(); 191 192 try { 193 // 2.从Properties对象中获取对应的配置信息 194 properties.load(in); 195 Class.forName(properties.getProperty("driverClass")); 196 } catch (IOException e) { 197 e.printStackTrace(); 198 } 199 catch (ClassNotFoundException e) { 200 e.printStackTrace(); 201 } 202 String url=properties.getProperty("url"); 203 String user=properties.getProperty("user"); 204 String password=properties.getProperty("password"); 205 // 获取Connection的连接对象 206 Connection connection = null; 207 Statement statement=null; 208 try { 209 connection = DriverManager.getConnection(url, user, password); 210 String sql_select="select * from student"; 211 // 获取Statement对象 212 statement = connection.createStatement(); 213 // 执行SQL语句 214 ResultSet rs = statement.executeQuery(sql_select); 215 while(rs.next()){ 216 int id= rs.getInt("id"); 217 String name=rs.getNString("name"); 218 int age=rs.getInt("age"); 219 System.out.println(id+" "+name+" "+age+"\n"); 220 } 221 } catch (SQLException throwables) { 222 throwables.printStackTrace(); 223 }finally { 224 if(connection!=null){ 225 try { 226 connection.close(); 227 } catch (SQLException throwables) { 228 throwables.printStackTrace(); 229 } 230 } 231 if(statement!=null){ 232 try { 233 statement.close(); 234 } catch (SQLException throwables) { 235 throwables.printStackTrace(); 236 } 237 } 238 } 239 240 } 241 }
5、
SELECT user, password FROM user_table WHERE user='a' OR 1 = 1' AND password ='123' OR 1 = 1)
查询

1 /** 2 * PreparedStatement实现查询操作 3 * @throws Exception 4 */ 5 @Test 6 public void select() throws Exception{ 7 // 1.加载配置文件 8 InputStream in = Test01.class.getClassLoader().getResourceAsStream("db.properties"); 9 Properties properties = new Properties(); 10 try { 11 properties.load(in); 12 // 2.从Properties对象中获取对应的配置信息 13 Class.forName(properties.getProperty("driverClass")); 14 } catch (IOException e) { 15 e.printStackTrace(); 16 } catch (ClassNotFoundException e){ 17 e.printStackTrace(); 18 } 19 String url = properties.getProperty("url"); 20 String userName = properties.getProperty("user"); 21 String password = properties.getProperty("password"); 22 Connection connection = DriverManager.getConnection(url, userName, password); 23 // 要使用PreparedStatement那么在SQL语句中我们需要使用?来占位 24 String sql = "select * from student where id = ?"; 25 // 通过Connection连接对象获取PreparedStatement对象 26 PreparedStatement preparedStatement = connection.prepareStatement(sql); 27 // 然后给占位符赋值3 28 preparedStatement.setInt(1,1); 29 // 执行SQL语句 30 ResultSet rs = preparedStatement.executeQuery(); 31 while(rs.next()){ 32 // 一次循环获取的就是一条记录 33 // 通过字段名称获取 34 /*int id = rs.getInt("id"); 35 String name = rs.getString("name"); 36 String sex = rs.getString("sex"); 37 String birth = rs.getString("birth"); 38 String department = rs.getString("department");*/ 39 40 // 还可以通过字段的位置来获取 注意下标从1开始 41 int id = rs.getInt(1); 42 String name = rs.getString(2); 43 int age = rs.getInt(3); 44 System.out.println(id+" " + name + " " +age); 45 } 46 //关闭相关资源 47 rs.close(); 48 preparedStatement.close(); 49 connection.close(); 50 }
常用的JDBC框架
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了