JDBC概述
JDBC概述
持久化:把数据保存到可掉电式存储设备中以供之后使用。数据持久化意味着将内存中的数据保存到硬盘上 加以“固化”,而持久化的实现过程大多通过各种关系数据库来完成。
持久化的主要应用是将内存中的数据存储在关系型数据库中,当然也可以存储在磁盘文件中、XML数据文件中
JDBC体系结构
JDBC接口(API)包括两个层次:
- 面向应用的API:Java API,抽象接口,供应用程序开发人员使用(连接数 据库,执行SQL语句,获得结果)
- 面向数据库的API:Java Driver API,供开发商开发数据库驱动程序用
JDBC程序编写步骤
获取数据库连接
连接数据库的方式
public class Connetion {
public static void main(String[] args) throws SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException, IOException {
testConnetion1();
System.out.println("=======================================");
testConnetion2();
System.out.println("=======================================");
testConnetion3();
System.out.println("=======================================");
textConnetion4();
System.out.println("=======================================");
testConnetion5();
}
//第一种连接方式
public static void testConnetion1() throws SQLException {
Driver driver = new com.mysql.cj.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/zxb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
//设置账户的账号密码
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","zxb02043113");
Connection Cnn = driver.connect(url,info);
System.out.println(Cnn);
}
//第二种连接方式 对方式一的迭代 为了程序有更好的可以执行 从而去除第三方的API
//通过反射
public static void testConnetion2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
//通过反射 实现获取Diver实现类对象
Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//提供连接的数据库
String url = "jdbc:mysql://localhost:3306/zxb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","zxb02043113");
Connection Cnn = driver.connect(url,info);
System.out.println(Cnn);
}
//第三种连接方式 使用DriverManager替换Driver (常用!!!)
public static void testConnetion3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
//获取Driver的实现类对象
Class aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//提供url user password
String url = "jdbc:mysql://localhost:3306/zxb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
String user = "root";
String password = "zxb02043113";
//注册驱动
DriverManager.registerDriver(driver);
//获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//第四中连接方式 对三进行优化
public static void textConnetion4() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
String url = "jdbc:mysql://localhost:3306/zxb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
String user = "root";
String password = "zxb02043113";
//Driver源码存在静态代码块所以只要加载Driver 就会默认使用DriverManager注册驱动
Class aClass = Class.forName("com.mysql.cj.jdbc.Driver"); //同样加载的MySQL代码也可以省略 驱动会自动加载Class 但是不推荐省略
// Driver driver = (Driver) aClass.newInstance();
// DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//获取连接的方式五 使用配置文件来设置配置信息 (究极完全体方式)
public static void testConnetion5() throws IOException, ClassNotFoundException, SQLException {
//读取配置文件中的基本信息
InputStream is = Connetion.class.getClassLoader().getResourceAsStream("jdbc.properties"); //使用系统类加载器
Properties properties = new Properties();
properties.load(is);//加载配置文件
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
//加载驱动
Class.forName(driverClass);
//获取连接
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
is.close();
}
}
配置文件信息:
user=root
password=zxb02043113
url=jdbc:mysql://localhost:3306/zxb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driverClass=com.mysql.cj.jdbc.Driver
使用PreparedStatement实现CRUD操作
操作和访问数据库
数据库连接被用于向数据库服务器发送命令和SQL语句,并接受数据库服务器返回的结果。其实一个数据库的连接就是一个Socket连接
在java.sql 包中有3个接口分别定义了对数据库的调用的不同方式:
Statement:用于执行静态SQL语句并返回它所生成结果的对象。
PreparedStatement:SQl语句被预编译并存储在此对象中,可以使用此对象多次高效地执行该语句
CallableStatement:用于执行SQL存储过程
使用Statement的弊端:需要拼写sql语句,并且存在SQL注入问题
为了避免出现SQL注入问题 所以用PreparedStatement替换Statement
实现功能样例
//连接数据库
Connection conn1 = get_jdbc.getConnection();
System.out.print("请输入你要添加信息的学号: (格式为:11226XX)");
String nu1 = sc.next();
System.out.print("请输入你要添加信息的姓名:");
String na1 = sc.next();
System.out.print("请输入你要添加信息的性别:");
String se1 = sc.next();
//预编译SQL语句 返回PreparedStatement实例
String sql1 = "insert into studentdata(Number,Name,Sex)values(?,?,?)"; //?:占位符
PreparedStatement preparedStatement1 = conn1.prepareStatement(sql1);
preparedStatement1.setString(1, nu1);
preparedStatement1.setString(2, na1);
preparedStatement1.setString(3, se1);
preparedStatement1.execute();
get_jdbc.closeConnection(conn1, preparedStatement1);
break;
case 2://修改指定记录
//连接数据库
Connection conn2 = get_jdbc.getConnection();
System.out.print("请输入你要修改信息的学号:(格式为:11226XX)");
String nu2 = sc.next();
System.out.print("请输入你将姓名修改后的结果:");
String na2 = sc.next();
String sql2 = "update studentdata set Name = ? where Number = ?";
PreparedStatement preparedStatement2 = conn2.prepareStatement(sql2);
preparedStatement2.setString(1, na2);
preparedStatement2.setString(2, nu2);
preparedStatement2.execute();
get_jdbc.closeConnection(conn2, preparedStatement2);
增删改通用方法:
public static void update(String sql,Object ... args) throws SQLException, IOException, ClassNotFoundException {
//连接
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = get_jdbc.getConnection();
//预编译
preparedStatement = connection.prepareStatement(sql);
//填充占位符
for(int i = 0; i < args.length; i++){
preparedStatement.setObject(i+1,args[i]);
}
//执行
preparedStatement.execute();
}catch (Exception e){
e.printStackTrace();
}finally {
//资源关闭
get_jdbc.closeConnection(connection,preparedStatement);
}
}