1. JDBC连接MySQL数据库
1.1 操作流程
1. 导入Jar包
在项目根目录创建lib目录,放入对应jar包,引入依赖
2. 加载驱动
Java程序只是规定了接口规范,但是没有实现
数据库连接需要使用JDBC对应驱动
3. 准备必要参数连接数据库
4. 获取数据库连接
5. 关闭资源
1.2 数据库连接演示代码
package com. qfedu;
import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. SQLException;
public class Demo1 {
public static void main ( String[ ] args) throws ClassNotFoundException, SQLException {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true" ;
String user = "root" ;
String password = "0920" ;
Connection connection = DriverManager. getConnection ( url, user, password) ;
System. out. println ( connection) ;
connection. close ( ) ;
}
}
1.3 数据库驱动加载过程
public class Driver extends NonRegisteringDriver implements java. sql. Driver {
static {
try {
java. sql. DriverManager. registerDriver ( new Driver ( ) ) ;
} catch ( SQLException E) {
throw new RuntimeException ( "Can't register driver!" ) ;
}
}
public Driver ( ) throws SQLException {
}
}
2. JDBC核心API
class java. sql . DriverManager 驱动管理类
interface java. sql . Connection 数据库连接接口
interface java. sql . Statement 数据库SQL 语句搬运工对象
interface java. sql . PreparedStatement 数据库SQL 语句【预处理】搬运工对象
PreparedStatement extends java. sql . Statement
interface java. sql . ResultSet 数据库结果集接口
3. Statement操作SQL语句
3.1 Statement插入SQL数据操作
@Test
public void testInsert ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true" ;
String user = "root" ;
String password = "0920" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "insert into nzgp2001.user(userName, password) value ('林博', '1234')" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
3.2 Statement修改SQL数据操作
@Test
public void testUpdate ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true" ;
String user = "root" ;
String password = "0920" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "update demo1.student set userName ='ljw' where id = 1" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
3.3 Statement删除SQL数据操作
@Test
public void testDelete ( ) {
Statement statement = null;
Connection connection = null;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
String url = "jdbc:mysql://localhost:3306/demo1?useSSL=true" ;
String user = "root" ;
String password = "0920" ;
connection = DriverManager. getConnection ( url, user, password) ;
statement = connection. createStatement ( ) ;
String sql = "delete from student where id > 2" ;
int affectedRows = statement. executeUpdate ( sql) ;
System. out. println ( "affectedRows:" + affectedRows) ;
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( statement != null) {
statement. close ( ) ;
}
if ( connection != null) {
connection. close ( ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}