Idea连接数据库(JDBC)
JDBC 固定步骤:
- 加载驱动
- 连接数据库,代表数据库
- 向数据库发送SQL的对象Statement : CRUD
- 编写SQL (根据业务,不同的SQL)
- 执行SQL
- 关闭连接
package com.wei.Teat;
import java.sql.*;
public class JdbcTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String username = "root";
String password = "123456";
//3.连接成功,数据库对象
Connection conn = DriverManager.getConnection(url, username, password);
//4.执行sql的对象
Statement statement = conn.createStatement();
//5.执行sql的对象 去执行sql ,可能存在结果,查看返回结果
String sql = "SELECT * FROM users";
ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部的查询处理啊的结果
while(resultSet.next()){
System.out.println("id=" + resultSet.getObject("id"));
System.out.println("name=" + resultSet.getObject("NAME"));
System.out.println("pwd=" + resultSet.getObject("PASSWORD"));
System.out.println("email=" + resultSet.getObject("email"));
System.out.println("birth=" + resultSet.getObject("birthday"));
System.out.println("==========================================");
}
//6.释放连接,先开的后关
resultSet.close();
statement.close();
conn.close();
}
}
工具类
1、创建资源文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudyuseSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username=root
password=123456
2、编写工具类
package com.wei.lesson04.Utils;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import javax.activation.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils_DBCP {
private static BasicDataSource dataSource = null;
static{
try{
//通过类加载器获得目标文件的资源
InputStream in = JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
//创建一个没有默认值的空属性列表。
Properties properties = new Properties();
//从输入流中读取属性列表
properties.load(in);
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnerction() throws SQLException {
return dataSource.getConnection();
}
//释放链接资源
public static void release(Connection conn, PreparedStatement preparedStatement, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
3、实际应用实例
package com.wei.lesson04;
import com.wei.lesson02.utils.JdbcUtils;
import com.wei.lesson04.Utils.JdbcUtils_DBCP;
import java.sql.*;
public class Test {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement preparedStatement = null;
//Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils_DBCP.getConnerction();
// st = conn.createStatement();
String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,email,birthday)" +
"VALUES('5','wei','123456','12348@qq.com','2020-1-1')";
int i = preparedStatement.executeUpdate(sql);
//int i = st.executeUpdate(sql);
if (i >0){
System.out.println("插入成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(conn,preparedStatement,rs);
}
}
}
!注:
Statement statement = conn.createStatement();是不安全的
(可能会发生sql注入等问题)PreparedStatement ps=ps.executeUpdate(sql);是安全的。