JDBC(Java DataBase Connectivity,java数据库链接)是一种用于执行SQL语句的Java API。JDBC是Java访问数据库的标准规范,能够为不一样的关系型数据库提供统一访问,它由一组用Java语言编写的接口和类组成。java
JDBC与数据库驱动的关系:接口与实现类的关系。mysql
JDBC有关的类:都在java.sql 和 javax.sql 包下. sql
接口在Java中是用来定义 `行为规范的`. 接口必须有实现类.数据库
JDBC规范(四个核心对象):服务器
DriverManager:用于注册驱动app
Connection: 表示与数据库建立的链接ide
Statement: 操做数据库sql语句的对象工具
ResultSet: 结果集或一张虚拟表atom
// JDBC 初体验 @Test public void demo01() throws SQLException { // 1. 装载驱动 DriverManager.registerDriver(new Driver()); // 2. 创建链接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "111"); // 3. 操做数据 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); } // 4. 释放资源 rs.close(); stmt.close(); conn.close(); }
// JDBC 初体验
@Test
public void demo01() throws SQLException {
// 1. 装载驱动
DriverManager.registerDriver(new Driver());
// 2. 创建链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "111");
// 3. 操做数据
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);
}
// 4. 释放资源
rs.close();
stmt.close();
conn.close();
}
// 配置文件的名字 jdbc.properties #mysql driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8 user=root password=111
// 配置文件的名字 jdbc.properties
#mysql
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8
user=root
password=111
public class JDBCUtils { // 属性 private static String driverClass; private static String url; private static String username; private static String password; // 何时加载外部配置文件最合适 ??? // 特色1 : 随着类的加载而加载. // 特色2 : 静态代码块只在类加载的被执行一次. 仅一次. 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 { // 1. 加载驱动 Class.forName(driverClass); } catch (ClassNotFoundException e) { // e.printStackTrace(); // 驱动加载失败! throw new RuntimeException("驱动加载失败!"); } } // 创建链接 public static Connection getConnection() throws SQLException { // 2. 创建链接 return DriverManager.getConnection(url, username, password); } // 释放资源 public static void release(Connection conn, Statement stmt, ResultSet rs) { // 4. 释放资源 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } // 将 rs 清空 rs = null; } // 直接调用 release(conn, stmt); } public static void release(Connection conn, Statement stmt) { // 4. 释放资源 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; } } }
public class JDBCUtils {
// 属性
private static String driverClass;
private static String url;
private static String username;
private static String password;
// 何时加载外部配置文件最合适 ???
// 特色1 : 随着类的加载而加载.
// 特色2 : 静态代码块只在类加载的被执行一次. 仅一次.
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 {
// 1. 加载驱动
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// e.printStackTrace();
// 驱动加载失败!
throw new RuntimeException("驱动加载失败!");
}
}
// 创建链接
public static Connection getConnection() throws SQLException {
// 2. 创建链接
return DriverManager.getConnection(url, username, password);
}
// 释放资源
public static void release(Connection conn, Statement stmt, ResultSet rs) {
// 4. 释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 将 rs 清空
rs = null;
}
// 直接调用
release(conn, stmt);
}
public static void release(Connection conn, Statement stmt) {
// 4. 释放资源
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;
}
}
}
@Test public void test_update() { Connection conn = null; Statement stmt = null; try { // 2. 创建链接 conn = JDBCUtils.getConnection(); // 3. 操做数据 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 { // 4. 释放资源 JDBCUtils.release(conn, stmt); } } @Test public void test_delete() { Connection conn = null; Statement stmt = null; try { // 1. 创建链接 conn = JDBCUtils.getConnection(); // 2. 操做数据 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 { // 4. 释放资源 JDBCUtils.release(conn, stmt); } } @Test public void test_insert() { Connection conn = null; Statement stmt = null; try { // 1. 创建链接 conn = JDBCUtils.getConnection(); // 2. 操做数据 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 { // 4. 释放资源 JDBCUtils.release(conn, stmt); } } // 以上使用时 在进行查询的操做时 有可能会出现 sql注入问题 // 解决SQL注入:使用PreparedStatement 取代 Statement // PreparedStatement 解决SQL注入原理,运行在SQL中参数以?占位符的方式表示 // select * from user where username = ? and password = ? ; // 将带有?的SQL 发送给数据库完成编译 (不能执行的SQL 带有?的SQL 进行编译 叫作预编译),在SQL编译后发现缺乏两个参数 // PreparedStatement 能够将? 代替参数 发送给数据库服务器,由于SQL已经编译过,参数中特殊字符不会当作特殊字符编译,没法达到SQL注入的目的 /************ JDBC 数据库链接操做 ***************/ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 1. 创建链接 conn = JDBCUtils.getConnection(); // 2. 操做数据 String sql = "select * from user where username = ? and password = ?;"; stmt = conn.prepareStatement(sql); // 设置sql语句的参数 stmt.setString(1, username); stmt.setString(2, password); // 执行sql语句 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 { // 3. 释放资源 JDBCUtils.release(conn, stmt, rs); } } }
x
public void test_update() {
Connection conn = null;
Statement stmt = null;
try {
// 2. 创建链接
conn = JDBCUtils.getConnection();
// 3. 操做数据
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 {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}
public void test_delete() {
Connection conn = null;
Statement stmt = null;
try {
// 1. 创建链接
conn = JDBCUtils.getConnection();
// 2. 操做数据
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 {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}
public void test_insert() {
Connection conn = null;
Statement stmt = null;
try {
// 1. 创建链接
conn = JDBCUtils.getConnection();
// 2. 操做数据
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 {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}
// 以上使用时 在进行查询的操做时 有可能会出现 sql注入问题
// 解决SQL注入:使用PreparedStatement 取代 Statement
// PreparedStatement 解决SQL注入原理,运行在SQL中参数以?占位符的方式表示
// select * from user where username = ? and password = ? ;
// 将带有?的SQL 发送给数据库完成编译 (不能执行的SQL 带有?的SQL 进行编译 叫作预编译),在SQL编译后发现缺乏两个参数
// PreparedStatement 能够将? 代替参数 发送给数据库服务器,由于SQL已经编译过,参数中特殊字符不会当作特殊字符编译,没法达到SQL注入的目的
/************ JDBC 数据库链接操做 ***************/
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 1. 创建链接
conn = JDBCUtils.getConnection();
// 2. 操做数据
String sql = "select * from user where username = ? and password = ?;";
stmt = conn.prepareStatement(sql);
// 设置sql语句的参数
stmt.setString(1, username);
stmt.setString(2, password);
// 执行sql语句
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 {
// 3. 释放资源
JDBCUtils.release(conn, stmt, rs);
}
}
}
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">url