首页>代码>JSP开发许愿墙模块源代码下载>/许愿墙模块/02/src/com/tools/ConnDB.java
package com.tools; //将该类保存到com.tools包中

import java.io.InputStream; //导入java.io.InputStream类
import java.sql.*; //导入java.sql包中的所有类
import java.util.Properties; //导入java.util.Properties类

public class ConnDB {
	public Connection conn = null; // 声明Connection对象的实例
	public Statement stmt = null; // 声明Statement对象的实例
	public ResultSet rs = null; // 声明ResultSet对象的实例
	private static String propFileName = "/com/connDB.properties"; // 指定资源文件保存的位置
	private static Properties prop = new Properties(); // 创建并实例化Properties对象的实例
	private static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//定义保存数据库驱动的变量
	private static String dbUrl = "jdbc:sqlserver://localhost:1433;DatabaseName=db_wishingWall";
	private static String dbUser = "sa";
	private static String dbPwd = "";
	public ConnDB() {	//定义构造方法
		try {			//捕捉异常
			//将Properties文件读取到InputStream对象中
			InputStream in = getClass().getResourceAsStream(propFileName);
			prop.load(in); // 通过输入流对象加载Properties文件
			dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动
			dbUrl = prop.getProperty("DB_URL", dbUrl);		//获取URL
			dbUser = prop.getProperty("DB_USER", dbUser);	//获取登录用户
			dbPwd = prop.getProperty("DB_PWD", dbPwd);		//获取密码
		} catch (Exception e) {
			e.printStackTrace(); // 输出异常信息
		}
	}

	public static Connection getConnection() {
		Connection conn = null;
		try {								//连接数据库时可能发生异常因此需要捕捉该异常
			Class.forName(dbClassName).newInstance();			//装载数据库驱动
			//建立与数据库URL中定义的数据库的连接
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (Exception ee) {
			ee.printStackTrace();									//输出异常信息
		}
		if (conn == null) {
			System.err
					.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
							+ dbClassName
							+ "\r\n链接位置:"
							+ dbUrl
							+ "\r\n用户/密码"
							+ dbUser + "/" + dbPwd);		//在控制台上输出提示信息
		}
		return conn;										//返回数据库连接对象
	}

	/*
	 * 功能:执行查询语句
	 */
	public ResultSet executeQuery(String sql) {
		try { // 捕捉异常
			conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);			//执行SQL语句,并返回一个ResultSet对象rs
		} catch (SQLException ex) {
			System.err.println(ex.getMessage()); // 输出异常信息
		}
		return rs; // 返回结果集对象
	}

	/*
	 * 功能:执行更新操作
	 */
	public int executeUpdate(String sql) {
		int result = 0; // 定义保存返回值的变量
		try { // 捕捉异常
			conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			result = stmt.executeUpdate(sql); // 执行更新操作
		} catch (SQLException ex) {
			result = 0; // 将保存返回值的变量赋值为0
		}
		return result; // 返回保存返回值的变量
	}
	//执行更新操作并返回新添加记录的自动编号值
	 public int executeUpdate_id(String sql) {
		    int result = 0; // 定义保存返回值的变量
		    try {
		      conn = getConnection();// 调用getConnection()方法构造Connection对象的一个实例conn
		      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
		                                  ResultSet.CONCUR_READ_ONLY);
		      result = stmt.executeUpdate(sql);		//执行更新操作,用于向数据表中插入数据
		      /*****************获取新添加记录的自动编号字段的值**********************/
		      String ID = "select @@IDENTITY as id";
		      rs = stmt.executeQuery(ID);
		      if (rs.next()) {
		        int autoID = rs.getInt("id");
		        result = autoID;
		      }
		      /******************************************************************/
		    }
		    catch (SQLException ex) {
		      result = 0;
		    }
		    return result;// 返回保存返回值的变量
		  }
	/*
	 * 功能:关闭数据库的连接
	 */
	public void close() {
		try { // 捕捉异常
			if (rs != null) { // 当ResultSet对象的实例rs不为空时
				rs.close(); // 关闭ResultSet对象
			}
			if (stmt != null) { // 当Statement对象的实例stmt不为空时
				stmt.close(); // 关闭Statement对象
			}
			if (conn != null) { // 当Connection对象的实例conn不为空时
				conn.close(); // 关闭Connection对象
			}
		} catch (Exception e) {
			e.printStackTrace(System.err); // 输出异常信息
		}
	}

}
最近下载更多
liu2022  LV14 2022年7月31日
wanglinddad  LV54 2022年6月7日
Start1  LV15 2021年10月30日
夏至  LV3 2021年6月9日
是菲菲菲啦啦  LV3 2021年3月23日
159878  LV2 2020年11月26日
ExamplesDYC  LV13 2020年11月22日
Snly小怪兽  LV4 2020年5月17日
Merlin12345  LV3 2020年3月25日
happy1231  LV7 2020年3月13日
最近浏览更多
Liang朝伟  LV1 1月5日
wttttts  LV2 1月1日
颜菜菜  LV2 2023年12月23日
yidaaaaaa  LV1 2023年5月10日
95959595959  LV13 2023年4月11日
微信网友_6411724627349504  LV3 2023年4月3日
yanmoumou  LV2 2022年12月22日
flyaing  LV1 2022年12月17日
485415187  LV6 2022年11月20日
gshnlj  LV15 2022年10月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友