Java连接数据库-jbdc
什么是jbdc
基本概念
关系图
操作步骤
jdbc 下载地址
jdbc驱动下载
bsq2
jdbc 使用步骤
获取连接器
项目中新建一个lib目录
设置lib 目录为库目录
让开放环境知道去哪里找相关文件 选中lib 目录右键它,选择 Add as Libary
注册驱动
获取连接对象
定义sql 语句和获取执行者
操作示例
实战编码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/hpschool";
Connection connection = DriverManager.getConnection(url, "root", "root");
Statement statement = connection.createStatement();
String sql = "INSERT INTO teacher VALUES(NULL,\"赵云\",\"龙胆\",\"2011-1-3\")";
int i = statement.executeUpdate(sql);
System.out.println(i);
}
}
jdbc 操做详解
相关对象
注册驱动
获得连接对象
连接对象的常用方法
执行者对象的相关方法
演练
增
删
改
查询结果集对象的使用
读取一行数据
读取所有行的数据
演练 查
import java.sql.*;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String connStr = "jdbc:mysql://localhost:3306/db11070326";
String user = "root";
String pass = "root123";
Connection conn = DriverManager.getConnection(connStr, user, pass);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM teacher;";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String phone = rs.getString("phone");
String skill = rs.getString("skill");
String res = String.format("编号:%s,姓名:%s,电话:%s,专业:%s",id,name,phone,skill);
System.out.println(res);
}
rs.close();
stmt.close();
conn.close();
}
}
事务操作
jbdc 工具类
分布代码
演练
工具类编写
阶段一
方式一
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String connStr = "jdbc:mysql://localhost:3306/db11070326";
String user = "root";
String pass = "root123";
conn = DriverManager.getConnection(connStr, user, pass);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
方式二
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
public static Connection getConnection() throws IOException {
Properties properties = new Properties();
FileReader fileReader = new FileReader("setting.properties");
properties.load(fileReader);
String connStr = properties.getProperty("url");
String user = properties.getProperty("user");
String pass = properties.getProperty("pass");
fileReader.close();
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(connStr, user, pass);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
利用properties集合存数据
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class pro集合 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("pass","root123");
properties.setProperty("url","jdbc:mysql://localhost:3306/db11070326");
FileWriter fileWriter = new FileWriter("setting.properties");
properties.store(fileWriter,null);
properties. close();
}
}
利用properties集合读数据
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class pro读数据 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileReader fileReader = new FileReader("setting.properties");
properties.load(fileReader);
String user = properties.getProperty("user");
String pass = properties.getProperty("pass");
String url = properties.getProperty("url");
System.out.println(user+pass+url);
fileReader.close();
}
}
静态方法块
阶段二
快捷键:Ctrl + Ait +T
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
private static Properties properties = new Properties();
static {
try {
FileReader fileReader = new FileReader("setting.properties");
properties.load(fileReader);
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String connStr = properties.getProperty("url");
String user = properties.getProperty("user");
String pass = properties.getProperty("pass");
conn = DriverManager.getConnection(connStr, user, pass);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
配置文件
#Thu Apr 07 16:14:58 CST 2022
pass=root123
user=root
url=jdbc\:mysql\://localhost\:3306/db11070326
安全执行者
安全问题 sql 注入
prepareStatement
数据为连接池
模型
概念
|