JDBC的复习(五):JDBC的封装
JDBC工具类的封装
- 封装
package com.bjpowernode.jdb;
import java.sql.*;
/**
* JDBC 工具类,简化JDBC程序
* @author 31200
*/
public class DbUtil {
/**
* 工具类中的构造方法都是私有的。
* 因为工具类当中的方法都是静态的,不需要new对象,直接采用类名调用
*/
private DbUtil(){}
//静态代码在类加载中执行,并且只执行一次
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode?characterEncoding=UTF8&useSSL=false","root","001204");
}
/**
* 关闭资源
* @param conn 连接对象
* @param ps 数据库操作对象
* @param rs 结果集
*/
public static void close(Connection conn, PreparedStatement ps,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
- 测试封装
package com.bjpowernode.jdb;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 1. 测试工具类
* 2. 测试模糊查询
* @author 31200
*/
public class JdbcTest11 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DbUtil.getConnection();
//获取数据库操作对象
String sql = "select ename from emp where ename like ?";
ps = conn.prepareStatement(sql);
ps.setString(1,"_A%");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("ename"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
DbUtil.close(conn,ps,rs);
}
}
}
测试成功!!
另一种封装:
- 在src目录下,创建一个db.properties文件
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/bjpowernode?characterEncoding=UTF8&useSSL=false
username = root
password = 001204
- 新建一个包存放工具类
package com.bjpowernode.utils;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @author 31200
*/
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try {
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//1. 驱动只要加载一次
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
/**
*
* @param conn
* @param ps
* @param rs
*/
public static void release(Connection conn, PreparedStatement ps, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
- 测试
package com.bjpowernode.jdb;
import com.bjpowernode.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 1. 测试工具类
* 2. 测试模糊查询
* @author 31200
*/
public class JdbcTest11 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
// conn = DbUtil.getConnection();
conn = JdbcUtils.getConnection();
//获取数据库操作对象
String sql = "select ename from emp where ename like ?";
ps = conn.prepareStatement(sql);
ps.setString(1,"_A%");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("ename"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
// DbUtil.close(conn,ps,rs);
JdbcUtils.release(conn,ps,rs);
}
}
}
通过测试!!!
用IDEA连接数据库
- 点击右上方的Database
- 点击+号
- 选择DataSource中的Mysql
- 输入账号和密码
- 测试连接
- 连接成功
-
如果报错:Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ prope
是因为时区设置不同!!
解决如下!
-
点击Advance
- 找到severTimezone,输入GMT,然后apply和ok就好了
- 连接成功,点击这个键可以进行刷新
- 点击这个键可以导入数据库
- 点击Schemas然后找到想要添加的数据应用就好