这一片接着上一篇的内容,仅仅是对上一次稍微的优化,所以这一次的代码并没有对数据库具体操作的语句,而只是封装函数的代码,预测下一期应该会出Java集合框架的基础知识
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class Utils {
//这个类用来将代码中需要重复进行非操作进行封装,使得代码更简单易懂
//首先封装要进行读取配置文件,获得数据库连接的前提条件
//配置文件引入的主要意义在于,当服务器修改是可以通过修改配置文件来重新初始化信息,而不需要修改源代码
private static final String drivername;
private static final String dburl;
private static final String dbusername;
private static final String dupassword;
static{//静态代码块是在类调用之前就执行的而且只执行一次,所以讲读取配置文件的操作放在静态代码块中
InputStream instream=null;
try{
instream=Utils.class.getClassLoader().getResourceAsStream("jdbcAndUtils/config.properties");
Properties prop =new Properties();
prop.load(instream);
drivername=prop.getProperty("drivername");
dburl=prop.getProperty("dburl");
dbusername=prop.getProperty("dbusername");
dupassword=prop.getProperty("dupassword");
}catch(IOException e){
throw new RuntimeException("加载");
}finally{
}
//按照数据库操作的步骤,第一步应该是加载驱动,而且也仅仅需要执行一次,所以就不妨把这一部分也写入静态代码块中
try {
Class.forName(drivername);
} catch (ClassNotFoundException e) {
System.err.println("驱动加载失败");
}
}//到此为止初始化信息已经结束,接下来要做的就是封装方法了
//加载驱动后的第一步操作应该是连接数据库,就是先建立一个连接,建立连接的函数
public static Connection createConnection() throws SQLException{
return DriverManager.getConnection("dburl","dbusername","dbpassword");
}
//接下来就是封装对数据库进行操作的方法了
//首先是executeUpdate()方法
//Object...parameter表示可变长度参数,在编译器处理时会将其按数组进行处理
//另外需要特别注意的是在对可变参数进行设置值的时候,参数的下边是从“1”开始的
public static int executeUpdate(Connection conn,String sql,Object...parameters)throws SQLException{
PreparedStatement stmt=null;
try{
stmt=conn.prepareStatement(sql);
for(int i=0;i<parameters.length;i++){
stmt.setObject(i+1, parameters[i]);
}
return stmt.executeUpdate();
}finally{
Utils.CloseQuiet(stmt);
}
}
public static int executeUpdate(String sql,Object...parameters)throws SQLException{
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=Utils.createConnection();
return executeUpdate(conn,sql,parameters);
}finally{
Utils.CloseQuiet(conn);
Utils.CloseQuiet(stmt);
}
}
public static ResultSet executeQuery(Connection conn,String sql,Object...parameters)throws SQLException{
PreparedStatement stmt=null;
try{
stmt=conn.prepareStatement(sql);
for(int i=0;i<parameters.length;i++){
stmt.setObject(i+1, parameters[i]);
}
return stmt.executeQuery();
}finally{
Utils.CloseQuiet(stmt);;
}
}
public static ResultSet executeQuery(String sql,Object...parameters)throws SQLException{
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=Utils.createConnection();
return executeQuery( conn, sql,parameters);
}finally{
Utils.CloseQuiet(conn);
Utils.CloseQuiet(stmt);
}
}
public static void CloseQuiet(Connection conn) {
if(conn==null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
public static void CloseQuiet(PreparedStatement stmt) {
if(stmt==null){
try {
stmt.close();
} catch (SQLException e) {
}
}
}}
浙公网安备 33010602011771号