JDBC基本开发步骤
一:注册驱动
方式一:DriverManager.registerDriver(new Driver()); //存在注册两次问题,性能较低,消耗资源
方式二:Class.forName("com.mysql.jdbc.Driver"); //开发中推荐
二:获取连接对象
//导入Java.sql包下COnnection
Connnection conn = DriverManager.getConnection();
三: 创建执行SQL语句的Statement对象
方式一:Statement stmt = conn.createStatement(); //存在SQL注入的问题,不推荐使用
方式二: Preparedstatement ps = conn.prepareStatement(); // 预编译对象,解决SQL注入问题
四:执行SQL语句
查询
ResultSet rs = ps.executeQuery(sql语句)
//判断结果是否存在结果 next()
while(rs.next()) {
//获取结果
getXxx(列的值)
getString(列的名)
}
增删改
executeUpdate(sql语句)
execute(sql) 结果是布尔类型,如果是查询操作返回true,增删改false
五:释放资源
查询: 关闭结果集 statement对象 connection对象
增删改: statement对象 connection对象
JDBC工具类编写
第一步:将工具类构造方法私有化
第二步:在工具类里面对外的访问方法,方法修饰为静态static的
public class JdbcUtils { //工具类的构造方法私有化
private JdbcUtils() { } //提供一个获取连接对象的方法
public static Connection getConnection() throws Exception {
//1,注册驱动
//DriverManager.registerDriver(new Driver()); //硬编码
Class.forName("com.mysql.jdbc.Driver");
//2,获取连接对象
String url = "jdbc:mysql://localhost:3306/heimatmall";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} //针对查询释放资源
public static void release(ResultSet rs,Statement stmt,Connection conn) { if(rs != null) { //判断对象不为空
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null; //Java自动垃圾机制,加快垃圾对象回收
} if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
} if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
} } //针对增删改释放资源
public static void release(Statement stmt,Connection conn) { if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
} if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
} } }
读取配置文件
方式一:使用Properties读取配置文件
步骤一:创建properties对象
步骤二:创建字节流对象,读取配置文件
步骤三: 将字节流对象传给properites对象。使用load方法关联
步骤四: 使用getProperty(“键的名字”)获取指定的值
public class MyJdbcUtils {
private static String driverName;
private static String url;
private static String user;
private static String password; //工具类的构造方法私有化
private MyJdbcUtils() { } //静态代码块:随着类的加载而加载,并且只执行一次
static {
//注册驱动
try {
//System.out.println("开始执行静态代码块里面的代码....读取配置文件完成注册驱动");
//第一步:创建Properties对象
Properties prop = new Properties();
//第二步:创建一个 字节流对象,关联要读取的properties文件
FileInputStream fis = new FileInputStream("src/jdbc.properties");
//第三步:传入字节流对象给properties对象
prop.load(fis);
//第四步:获取数据
driverName = prop.getProperty("DriverName");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//注册驱动
Class.forName(driverName);
} catch (Exception e) { //异常处理快捷键:alt++shift+z
// TODO Auto-generated catch block
e.printStackTrace();
}
} //提供一个获取连接对象的方法
public static Connection getConnection() throws Exception {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} //针对查询释放资源
public static void release(ResultSet rs,Statement stmt,Connection conn) { if(rs != null) { //判断对象不为空
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null; //Java自动垃圾机制,加快垃圾对象回收
} if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
} if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
} } //针对增删改释放资源
public static void release(Statement stmt,Connection conn) { if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
} if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
} } }
方式二:使用ResourceBundle对象读取
//扩展:使用ResourceBundle读取,注意:读取的目录是src下面的properties文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
driverName = bundle.getString("DriverName");
url = bundle.getString("url");
user = bundle.getString("user");
password = bundle.getString("password");
Class.forName(driverName);