第一章 JDBC简介
- Java DataBase Connection,一套Java操作数据库的接口的规范
1. JDBC的好处
- 我们只需要会调用JDBC接口中的方法即可,使用简单
- JDBC有关的类和接口:都在java.sql 和 javax.sql(扩展包) 包下
- 方法体由具体的数据库厂商来完成的
- 使用同一套Java代码,进行少量的修改就可以访问其他JDBC支持的数据库
第二章 JDBC使用
1. 使用步骤:
- 下载MySQL for Java 驱动 Maven仓库,add as library
- 加载驱动
- 建立连接
- 编写SQL语句
- 创建执行sql的Statement对象,执行sql(增删改查)
- 最后关闭资源,statement、connection、resultSet.
2. 普通方式实现代码
package mysql_use;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class Demo01 {
public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
conMySQL();
conMySQL2();
}
public static void conMySQL() throws SQLException, ClassNotFoundException, IOException {
Properties dbConfig = getProperties();
String driverClassName = dbConfig.getProperty("driverClassName");com.mysql.cj.jdbc.Driver
String url = dbConfig.getProperty("url");
String username = dbConfig.getProperty("username");
String password = dbConfig.getProperty("password");
Class.forName(driverClassName);
Connection connection = DriverManager.getConnection(url, username, password);
String user = "root";
String pwd = "123456";
String sql = "select * from emp where username = ? and password = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user);
preparedStatement.setString(2, pwd);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
double salary = resultSet.getDouble(6);
double bonus = resultSet.getDouble(7);
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("salary:" + salary);
System.out.println("bonus:" + bonus);
System.out.println("=========================");
}
resultSet.close();
preparedStatement.close();
connection.close();
}
public static void conMySQL2() throws SQLException, ClassNotFoundException, IOException {
Properties dbConfig = getProperties();
String driverClassName = dbConfig.getProperty("driverClassName");
String url = dbConfig.getProperty("url");
String username = dbConfig.getProperty("username");
String password = dbConfig.getProperty("password");
Class.forName(driverClassName);
Connection connection = DriverManager.getConnection(url, username, password);
String user = "root";
String pwd = "123456";
String sql = "select * from emp where username = '"+user+"'" + "and password='" + pwd + "'";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
double salary = resultSet.getDouble(6);
double bonus = resultSet.getDouble(7);
System.out.println("id:" + id);
System.out.println("name:" + name);
System.out.println("salary:" + salary);
System.out.println("bonus:" + bonus);
System.out.println("=========================");
}
resultSet.close();
statement.close();
connection.close();
}
private static Properties getProperties() throws IOException {
InputStream inputStream = Demo01.class.getClassLoader().getResourceAsStream("db.properties");
Properties dbConfig = new Properties();
dbConfig.load(inputStream);
return dbConfig;
}
}
3. 优化为工具类
package mysql_use;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.*;
public class JDBCUtil {
private static String driverClassName;
private static String url;
private static String username;
private static String password;
static {
Properties dbConfig = getProperties();
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
driverClassName = dbConfig.getProperty("driverClassName");
url = dbConfig.getProperty("url");
username = dbConfig.getProperty("username");
password = dbConfig.getProperty("password");
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
private static Properties getProperties() {
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties dbConfig = new Properties();
try {
dbConfig.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return dbConfig;
}
}
4. 使用数据库连接池Druid
package mysql_use;
import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.*;
public class DruidUtil {
private static DataSource dataSource;
static {
Properties dbConfig = getProperties();
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(dbConfig.getProperty("driverClassName"));
druidDataSource.setUrl(dbConfig.getProperty("url"));
druidDataSource.setUsername(dbConfig.getProperty("username"));
druidDataSource.setPassword(dbConfig.getProperty("password"));
dataSource = druidDataSource;
}
private static Properties getProperties() {
try {
InputStream inputStream = DruidUtil.class.getClassLoader().getResourceAsStream("db.properties");
Properties dbConfig = new Properties();
dbConfig.load(inputStream);
if (inputStream != null) inputStream.close();
return dbConfig;
} catch (Exception e) {
throw new RuntimeException();
}
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
第三章 参考资料
B站黑马程序员
|