jdbc
时间:2021-07-01 10:21:17
帮助过:4人阅读
controllers.CarSecurityCheckPlugin.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public final class ConnUtils {
private static String driverName;
private static String dbURL;
private static String userName;
private static String userPwd;
private ConnUtils() {
}
static {
// driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
// dbURL="jdbc:sqlserver://192.168.20.245:1433;DatabaseName=anjianduijie";
// userName="sa";
// userPwd="123";
Properties pro =
new Properties();
try {
pro.load(new FileInputStream("modules/CarSecurityCheckPlugin/conf/jdbc.properties"
));
} catch (IOException e1) {
e1.printStackTrace();
}
driverName = pro.getProperty("jdbc.driverName"
);
dbURL = pro.getProperty("jdbc.dbURL"
);
userName = pro.getProperty("jdbc.userName"
);
userPwd = pro.getProperty("jdbc.userPwd"
);
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
Connection dbConn =
DriverManager.getConnection(dbURL, userName, userPwd);
return dbConn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 释放资源
*
* @param rs
* @param st
* @param conn
*/
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs !=
null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st !=
null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn !=
null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
jdbc.properties文件里面写
jdbc.driverName=com.microsoft.sqlserver.jdbc.SQLServerDriver这些配置。
jdbc
标签: