目录
JDBC
jdbc 定义规范的接口,具体的实现有各大数据库的厂商来实现。
jdbc是java访问数据的便准规范。
jdbc的好处
1. 程序员开发访问数据库程序,只需要会调用jdbc接口中方法即可,具体如何实现的,可以忽略。
2. 使用同一套java代码,进行少量修改可以访问其他jdbc支持的数据库。
基本步骤
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.导入驱动jar包(是由数据库厂商提供)
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest?serverTimezone=UTC", "root", "123456");
//url:连接数据库的地址 username:用户名 password:密码
//4.定义sql语句
String sql = "update emp set salary = 2000 where id = 1";
//5.获取执行sql的对象
Statement stmt = conn.createStatement();
//6.执行sql语句
int count = stmt.executeUpdate(sql);
//7.处理结果
System.out.println(count);
//8.释放资源
stmt.close();
conn.close();
}
}
mysql Connector/J 5.x 版本连接方式
url=jdbc:mysql://localhost:3306/jdbctest driver=com.mysql.jdbc.Driver
Mysql 8.x版本 mysql Connector/J 6.x 版本之后连接方式
url=jdbc:mysql://localhost:3306/db4?serverTimezone=UTC driver=com.mysql.cj.jdbc.Driver
1.使用JDBC进行简单的插入操作
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.导入驱动jar包(是由数据库厂商提供)
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123456");
//url:连接数据库的地址 username:用户名 password:密码
//4.定义sql语句
String sql = "insert into account values(null,'李四',4000)";
//5.获取执行sql的对象
stmt = conn.createStatement();
//6.执行sql语句
int count = stmt.executeUpdate(sql);
//7.处理结果
System.out.println(count);
if (count > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//8.释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2.使用jdbc进行简单的查询操作
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.导入驱动jar包(是由数据库厂商提供)
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库连接对象
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;//结果集对象。
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123456");
//url:连接数据库的地址 username:用户名 password:密码
//4.定义sql语句
String sql = "select * from account";
//5.获取执行sql的对象
stmt = conn.createStatement();
//6.执行sql语句
rs = stmt.executeQuery(sql);
//7.处理结果
if (rs != null) {
while (rs.next()) {
//获取数据值
int id = rs.getInt("id");
String name = rs.getString("name");
double balance = rs.getDouble("balance");
System.out.println(id + "---" + name + "---" + balance);
}
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//8.释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Account类设计
public class Account {
private Integer id;
private String name;
private Double balance;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
public Account(Integer id, String name, Double balance) {
this.id = id;
this.name = name;
this.balance = balance;
}
public Account() {
}
}
JDBC工具类设计
1.配置文件(.properties)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbctest username=root password=123456
2.JDBCUtils类设计
public class JDBCUtils {
private static String url;
private static String username;
private static String password;
private static String driver;
static {
try {
//类被加载时就执行静态代码块,并且只执行一次。
//1.Properties集合对象
Properties pro = new Properties();
//获取src路径下的文件方式 --> ClassLoader 类加载器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbcConfig.properties");
String path = res.getPath();
System.out.println(path);
pro.load(new FileReader(path));
//获取集合中所有值进行属性赋值操作
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
// 驱动的注册
Class.forName(driver);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement stmt, Connection conn, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
close(stmt,conn);
}
}