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();//使用 PreparedStatement 预编译sql
conMySQL2();//使用 Statement
}
//使用 PreparedStatement 预编译sql
public static void conMySQL() throws SQLException, ClassNotFoundException, IOException {
//1.从配置文件中读取 连接配置,用户名,密码
Properties dbConfig = getProperties();
String driverClassName = dbConfig.getProperty("driverClassName");com.mysql.cj.jdbc.Driver
String url = dbConfig.getProperty("url");//jdbc:mysql://ip:3306/数据库名
String username = dbConfig.getProperty("username");//MySQL数据库用户名
String password = dbConfig.getProperty("password");//MySQL数据库密码
//2.加载驱动类 com.mysql.cj.jdbc.Driver类,
//实际是为了执行Driver类中的静态代码块{ DriverManager.registerDriver(new Driver()); }注册MySQL驱动
Class.forName(driverClassName);
//3.建立连接,给定参数(某个数据库,用户名,密码)
Connection connection = DriverManager.getConnection(url, username, password);
//4.编写sql语句
String user = "root";//模拟传入的用户名
String pwd = "123456";//模拟传入的密码
String sql = "select * from emp where username = ? and password = ?";// ?占位符,具体内容先不写,由PreparedStatement对象.setXX()方法 设置
//4.创建 预编译SQL语句的PreparedStatement对象 ( 防止SQL注入风险,提高PerparedStatement对象的复用性)
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user);//第一个?占位符 用usen值替换
preparedStatement.setString(2, pwd); //第二个?占位符 用pwd值替换
//5. 执行sql,得到结果集
ResultSet resultSet = preparedStatement.executeQuery();
//6. 打印结果
while (resultSet.next()) { //next() 初始指向 row 0 字段栏 ,方法执行完移动到下一行
//此时next()指向 row 1
//根据字段的数据具体类型,返回对应的数据类型
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("=========================");
}
//7.关闭资源
resultSet.close();
preparedStatement.close();
connection.close();
}
//使用 Statement
public static void conMySQL2() throws SQLException, ClassNotFoundException, IOException {
//1.从配置文件中读取 连接配置,用户名,密码
Properties dbConfig = getProperties();
String driverClassName = dbConfig.getProperty("driverClassName");
String url = dbConfig.getProperty("url");
String username = dbConfig.getProperty("username");
String password = dbConfig.getProperty("password");
//2.加载驱动类 com.mysql.cj.jdbc.Driver类,
//实际是为了执行Driver类中的静态代码块{DriverManager.registerDriver(new Driver());}注册MySQL驱动
Class.forName(driverClassName);
//3.建立连接,给定参数(某个数据库,用户名,密码)
Connection connection = DriverManager.getConnection(url, username, password);
//4.编写sql语句
String user = "root";//模拟传入的用户名
String pwd = "123456";//模拟传入的密码
String sql = "select * from emp where username = '"+user+"'" + "and password='" + pwd + "'";//登录校验
//4.创建 执行静态SQL语句的Statement对象
//或创建 预编译SQL语句的PreparedStatement对象 防止SQL注入风险
Statement statement = connection.createStatement();
//5. 执行sql,返回结果集
ResultSet resultSet = statement.executeQuery(sql);
//6. 打印结果
while (resultSet.next()) { //next() 初始指向 row 0 字段栏 ,方法执行完移动到下一行
//此时next()指向 row 1
//根据字段的数据具体类型,返回对应的数据类型
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("=========================");
}
//7.关闭资源
resultSet.close();
statement.close();
connection.close();
}
//获取资源文件输入流,并存到Properties集合中
private static Properties getProperties() throws IOException {
InputStream inputStream = Demo01.class.getClassLoader().getResourceAsStream("db.properties");
Properties dbConfig = new Properties();
dbConfig.load(inputStream);
return dbConfig;
}
}
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); //为了执行Driver类中的静态代码块 注册驱动
} 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);
}
//获取资源文件输入流,并存到Properties集合中
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;
}
}
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();//使用连接池
//设置 驱动路径、url、用户、密码
druidDataSource.setDriverClassName(dbConfig.getProperty("driverClassName"));
druidDataSource.setUrl(dbConfig.getProperty("url"));
druidDataSource.setUsername(dbConfig.getProperty("username"));
druidDataSource.setPassword(dbConfig.getProperty("password"));
dataSource = druidDataSource;
}
//获取资源文件输入流,并存到Properties集合中
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站黑马程序员