JDBC的概念
概念;Java DataBase Connectivity Java数据库连接,Java语言皂搓数据库
本质:其实是sun公司定义的一套做错所有关系型数据库的规则,是一个接口,所有的数据库厂商去实现这一套接口,提供数据库驱动jar包(字节码文件),我们就可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类.(采用多态方式调用)
使用步骤:
1.导入驱动jar包
2.注册驱动
3.获取数据库连接的对象:Connection
4.定义SQL语句
5.获取执行SQL语句的对象Statement
6.执行SQL语句,接收返回结果集ResultSet
7.处理结果集
8.关闭资源
public class jdbcDemo01 {
public static void main(String[] args) throws Exception {
// 1.导入驱动jar包
// 2.注册驱动
//旧版本
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
// 3.获取数据库连接的对象:Connection
//旧版本
// Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","9672005");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false","root","9672005");
// 4.定义SQL语句
String sql = "update user set name = 'rong' where id = 1";
// 5.获取执行SQL语句的对象Statement
Statement statement = connection.createStatement();
// 6.执行SQL语句,接收返回结果集ResultSet
int count = statement.executeUpdate(sql);
// 7.处理结果集
System.out.println(count);
// 8.关闭资源
statement.close();
connection.close();
}
}
这里的新版本好像是我的数据库在5以上.旧版本会报错
ResultSet结果集–封装查询结果
- next() 游标向下移动一行 ,判断是不是表的最后一行
- getXxx() 获取数据 xxx 类型,可以传入第几列,也可以传入字段名
public class jdbcDemo02 {
public static void main(String[] args) {
Connection connection =null;
Statement statement= null ;
ResultSet resultSet = null ;
// 1.导入驱动jar包
// 2.注册驱动
//旧版本
//Class.forName("com.mysql.jdbc.Driver");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 3.获取数据库连接的对象:Connection
//旧版本
// Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","9672005");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false","root","9672005");
// 4.定义SQL语句
String sql = "select * from user ";
// 5.获取执行SQL语句的对象Statement
statement = connection.createStatement();
// 6.执行SQL语句,接收返回结果集ResultSet
resultSet = statement.executeQuery(sql);
// 7.处理结果集
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id+"---"+name);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
// 8.关闭资源
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();
}
}
}
}
}
抽取JDBC工具类:JDBCUtils
目的:简化书写
分析:
- 抽取驱动方法
- 建立连接对象方法
- 释放资源方法
public class jdbcUtils {
private static String url ;
private static String user ;
private static String password ;
private static String dirver ;
Connection connection =null;
Statement statement= null ;
ResultSet resultSet = null ;
static{
try {
//创建properties集合类
Properties pro = new Properties();
//获取SRC路径下的文件方式,--->CLassLoader 类加载器
ClassLoader classLoader = jdbcUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path =res.getPath();
System.out.println(path);
//加载文件
pro.load(new FileReader(path));
//获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password=pro.getProperty("password");
dirver = pro.getProperty("dirver");
try {
//加载驱动
Class.forName(dirver);
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(Statement stmt,Connection conn){
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement stmt,Connection conn,ResultSet resultSet){
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
从这里就可以看出JDBCTtulis 工具类中,对外展示了一个getConnection的方法,close方法(),通过这些方法,可以得到数据库的连接对象connection,并且可以关闭资源.