方式一(不常用)
注册驱动
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCTest01{
public static void main(String[] args){
try{
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型指向子类型对象
DriverManager.registerDriver(driver);
}catch(SQLException e){
e.printStackTrace();
}
}
}
获取连接
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
public class JDBCTest01{
public static void main(String[] args){
try{
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型指向子类型对象
DriverManager.registerDriver(driver);
//获取连接
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
String user = "root";
String password = "qqqxdd666";
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象:" + conn);
}catch(SQLException e){
e.printStackTrace();
}
}
}
获取数据库操作对象
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
public class JDBCTest01{
public static void main(String[] args){
try{
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型指向子类型对象
DriverManager.registerDriver(driver);
//获取连接
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
String user = "root";
String password = "qqqxdd666";
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象:" + conn);
//获取数据库操作对象(Statement专门执行sql语句)
Statement stmt = con.createStatement();
}catch(SQLException e){
e.printStackTrace();
}
}
}
执行sql语句
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
public class JDBCTest01{
public static void main(String[] args){
try{
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型指向子类型对象
DriverManager.registerDriver(driver);
//获取连接
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
String user = "root";
String password = "qqqxdd666";
Connection con = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象:" + conn);
//获取数据库操作对象(Statement专门执行sql语句)
Statement stmt = con.createStatement();
//执行sql
String sql = "insert into dept(deptno, dname, loc) values(50, '人事部', '北京')";
//专门执行DML语句的(insert,delete,update)
//返回值是“影响数据库中的记录条数”
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失败")
}catch(SQLException e){
e.printStackTrace();
}
}
}
处理查询结果集
import java.sql.*
public class JDBCTest03{
public static void main(String[] args){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forname("com.mysql.jdbs.Driver");
con = DriverManager.getConnection(url,user,password);
stmt = con.createStatement();
String sql = "select empno,ename,sal from emp";
rs = stmt.executeQuery(sql);//专门执行select查询语句
while(rs.next()){
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(stmt != null){
try{
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(con != null){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
释放资源
- 在finally语句块中关闭资源
- 遵循从小到大的顺序,依次关闭
- 分别对其 try,…catch…
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
public class JDBCTest01{
public static void main(String[] args){
Connection con = null;
Statement stmt = null;
try{
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型指向子类型对象
DriverManager.registerDriver(driver);
//获取连接
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
String user = "root";
String password = "qqqxdd666";
con = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象:" + conn);
//获取数据库操作对象(Statement专门执行sql语句)
stmt = con.createStatement();
//执行sql
String sql = "insert into dept(deptno, dname, loc) values(50, '人事部', '北京')";
//专门执行DML语句的(insert,delete,update)
//返回值是“影响数据库中的记录条数”
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失败")
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(stmt != null){
stmt.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try{
if(con != null){
con.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
方式二:类加载的方式注册驱动(常用)
import java.sql.*;
public class JDBCTest02{
public static void main(String[] args){
try{
Class.forName("com.mysql.jbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
System.out.println(con);
}catch(Exception e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
将连接数据库的所有信息配置到配置文件中
import java.sql.*;
public class JDBCTest01{
public static void main(String[] args){
//使用资源绑定器绑定配置文件
ResourceBundle bundle = RescourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
Connection con = null;
Statement stmt = null;
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
//获取连接
String url = "jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC";
String user = "root";
String password = "qqqxdd666";
con = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象:" + conn);
//获取数据库操作对象(Statement专门执行sql语句)
stmt = con.createStatement();
//执行sql
String sql = "insert into dept(deptno, dname, loc) values(50, '人事部', '北京')";
//专门执行DML语句的(insert,delete,update)
//返回值是“影响数据库中的记录条数”
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失败")
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt != null){
stmt.close();
}
}catch(SQLException e){
e.printStackTrace();
}
try{
if(con != null){
con.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
以下进行配置文件的处理:
jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/DBNAME?serverTimezone=UTC
user=root
password=qqqxdd666
解决SQL注入问题
使用PreparedStatement。
- 解决注入问题。
- 编译一次,执行n次。效率更高。
- 在编译阶段做类型的安全检查。
PS:在项目需要进行SQL语句拼接时,需要使用支持SQL注入的Statement。
package com.logicvan.jdbc;
/*
实现功能:
1、需求:模拟用户登录功能
2、业务描述:
程序运行的时候,提供一个输入入口,可以让用户输入用户名和密码。
输入用户名和密码之后,提交信息,java程序收集信息。
java程序连接数据库验证用户名和密码是否合法。
*/
import java.sql.*;
public class JDBCTest03 {
public static void main(String[] args){
String name = userInfo.get("userName");
String password = userInfo.get("userPassword");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowercode?serverTimezone=UTC","root","qqqxdd666");
//SQL语句框架,?代表一个占位符,?代表未来一个值
String sql = "select * from t_users where login_name = ? and login_password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return flag;
}
}
}
事务机制
JDBC的事务是自动提交,执行一条语句,则自动提交一次。
conn.setAutoCommit(false);
conn.commit();
conn.rollback();//try...catch...加上