JDBC详细步骤
正常步骤
public class DemoStatement {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
Properties pro = new Properties();
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
// 1.加载驱动
Class.forName(pro.getProperty("driver"));
//2.建立连接
Connection connection = DriverManager.getConnection(
pro.getProperty("url"),
pro.getProperty("username"),
pro.getProperty("password")
);
//准备数据
String sql = "select * from dept";
//3.创建处理快
Statement statement = connection.createStatement();
//4.执行sql
ResultSet resultSet = statement.executeQuery(sql);
//5.处理结果集
while (resultSet.next()){
int dptno = resultSet.getInt(1);
String dname = resultSet.getNString(2);
String loc = resultSet.getNString(3);
System.out.println(dname+"-->"+dptno+"-->"+loc);
}
//6.释放资源
resultSet.close();
statement.close();
connection.close();
}
}
封装
能封装的有三个
- 加载驱动
- 获取连接
- 关闭资源
public class Util {
//1.封装加载驱动
private static Properties pro = new Properties();
static {
try {
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//2.封装获取连接
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(
pro.getProperty("url"),
pro.getProperty("username"),
pro.getProperty("password")
);
return connection ;
}
//3.封装关闭资源
public static void close(ResultSet resultSet, Statement statement,Connection connection){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close (Statement statement ,Connection connection){
close(null,statement,connection);
}
实现用户登录功能
静态处理快实现
此为静态处理块
public static boolean reg(String username ,String password){
boolean flag = false;
//申明
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
//1.加载驱动获得连接
try {
connection = Util.getConnection();
//准备sql
String sql = "select * from t_user where username ='"+username+"'and password = '"+password+"'";
//2.创建处理块
statement = connection.createStatement();
//3.发送sql
resultSet = statement.executeQuery(sql);
//4.处理结果
if (resultSet.next()){
flag = true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//5.关闭资源
Util.close(resultSet,statement,connection);
}
return flag;
}
预处理块实现
public static boolean login(String username ,String password){
boolean flag = false;
//申明
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement statement = null;
try {//获取连接
connection = Util.getConnection();
//准备sql
String sql = "select * from t_user where username = ? and password = ? ";
//创建处理快
statement = connection.prepareStatement(sql);
//为占位符赋值
statement.setObject(1,username);
statement.setObject(2,password);
//执行sql
resultSet = statement.executeQuery();
//处理结果
if (resultSet.next()){
flag = true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
Util.close(resultSet,statement,connection);
}
return flag;
}
两者之间区别
- 预处理快能够防止sql注入
- 预处理快效率高