package com.mingrisoft; //指定类所在的包 import java.io.InputStream; import java.sql.*; import java.util.Properties; public class DBConnection { private String FileName; //配置文件名 private int DBType; //数据库类型 private String MySqlDriver; //MYSQL Server驱动程序 private String MySqlURL; //MYSQL Server连接字符串 private String username; //用户名 private String password; //密码 public Connection conn = null; //数据库连接对象 public Statement stmt = null; //Statement对象,用于执行SQL语句 public ResultSet rs = null; //结果集对象 public DBConnection() { conn = null; } public Connection getConn() { DBType= new Function().StrToInt(getPara("DBType")); switch(DBType) { case 1:return(getConnToMySql()); default:return null; } } public String getPara(String ParaName) { FileName="../DBConfig.property"; Properties prop= new Properties(); try { InputStream is=getClass().getResourceAsStream(FileName); prop.load(is); if(is!=null) is.close(); } catch(Exception e) { return "Error!"; } return prop.getProperty(ParaName); } public Connection getConnToMySql() { try{ MySqlDriver = getPara("MySQLDriver"); MySqlURL = getPara("url"); username = getPara("username"); password = getPara("password"); Class.forName(MySqlDriver).newInstance(); conn = DriverManager.getConnection(MySqlURL,username,password); }catch(Exception e){ e.printStackTrace(); //return "操作数据库出错,请仔细检查" ; //System.err.println(e.getMessage()); } return conn; } /** * 功能:关闭数据库连接 */ public void close() { try { // 捕捉异常 if (rs != null) { rs.close();// 关闭结果集对象 } if (stmt != null) { stmt.close(); // 关闭Statement对象 } if (conn != null) { conn.close(); // 关闭数据库连接对象 } } catch (Exception e) {// 处理异常 e.printStackTrace(System.err);// 输出异常信息 } } /** * 功能:更新数据 * * @param sql * @return */ public int executeUpdate(String sql) { int result = 0;// 更新数据的记录条数 try {// 捕捉异常 conn = getConnToMySql();// 获取数据库连接 // 创建用于执行SQL语句的Statement对象 stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); result = stmt.executeUpdate(sql);// 执行SQL语句 } catch (SQLException ex) {// 处理异常 result = 0;// 指定更新数据的记录条数为0,表示没有更新数据 ex.printStackTrace();// 输出异常信息 } try {// 捕捉异常 stmt.close();// 关闭用于执行SQL语句的Statement对象 } catch (SQLException ex1) {// 处理异常 ex1.printStackTrace();// 输出异常信息 } return result;// 返回更新数据的记录条数 } public static void main(String[] args) { new DBConnection().getConnToMySql(); } }

ma406805131 LV19
2024年12月26日
张德志 LV9
2024年6月12日
顾北城 LV12
2024年3月28日
yyidaaa LV1
2023年12月14日
223664015 LV6
2023年6月14日
2063066273 LV2
2023年6月14日
1379585889 LV11
2023年6月12日
996171721 LV7
2023年5月29日
mmmkkl LV2
2023年5月10日
Honliy LV4
2023年2月6日

F丶S丶H LV7
昨天
ryadmin123 LV2
2月20日
cassie555 LV3
1月21日
zongbaoli LV2
1月7日
ma406805131 LV19
2024年12月18日
zolscy LV24
2024年11月29日
小黄同学 LV8
2024年9月29日
15719908287 LV9
2024年6月19日
张德志 LV9
2024年6月12日
chutianyu LV4
2024年6月6日