属性文件连接数据库
public class DBConnection {
private static ReadPropertIEs readPro = null;
// 加载驱动
static {
try {
readPro = ReadPropertIEs.getInstance();
Class.forname(readPro.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printstacktrace();
}
}
// 连接数据库
public Connection getConnection() throws sqlException {
return DriverManager.getConnection(readPro.getProperty("url"),readPro
.getProperty("username"),readPro.getProperty("password"));
}
// 查询方法
public Result excutequery(Connection con,String sql,Object[] params)
throws sqlException {
Result r = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setobject(i + 1,params[i]);
}
rs = pstmt.executequery();
r = ResultSupport.toResult(rs);
} finally {
pstmt.close();
con.close();
}
return r;
}
// 增删改方法 public int excuteUpdate(Connection con,Object[] params) throws sqlException { int num = 0; PreparedStatement pstmt = con.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstmt.setobject(i + 1,params[i]); } num = pstmt.executeUpdate(); pstmt.close(); con.close(); return num; }}
总结以上是内存溢出为你收集整理的SQLServer数据库连接(二)全部内容,希望文章能够帮你解决SQLServer数据库连接(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)