⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconn.java

📁 旅游管理系统
💻 JAVA
字号:
/*
 * 打开数据库连接
 * 选择查询
 */
package com.bjfu.tm;

import java.sql.*;

import javax.sql.DataSource;
import javax.sql.rowset.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.*;

public class DbConn {
	private Connection conn = null;

	private Statement stmt = null;
	
	DataSource ds = null;

	private ResultSet rs = null;

	/*String strDriverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // ?????????

	String strURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=selSub"; // ????l????

	String user = "sa"; // ?????????

	String password = "sa"; // ????????
	*/

	public DbConn() {

	}

	// 打开数据库连接??bl??
	public void OpenConn() throws Exception {
		try {
			InitialContext initCtx = new InitialContext();
			Context ctx = (Context) initCtx.lookup("java:comp/env");
			/*下面语句查找name值为jdbcsqlserver的数据源,即我们在开始的时候
			*定义过的.<br>
			*/
			ds = (DataSource) ctx.lookup("jdbc/sqlserver");
			//获取连接后就可以使用这个连接了
			conn = ds.getConnection();

		} catch (Exception ex) {
			System.err.println("出错信息为:" + ex.getMessage());
			ex.printStackTrace();
		}

	}

	/*
	 * ?????????????????
	 */
	public Statement getStmtRead() {
		try {
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
		} catch (Exception e) {
			System.out.println("getStmtRead");
			System.out.println(e.getMessage());
		}
		return stmt;
	}

	/*
	 * ?????????????
	 */
	/*
	 * public int select(String sql) { int k=-10; try { k=0; rs =
	 * this.getStmtRead().executeQuery(sql); while(rs.next()) { k++; } }
	 * catch(Exception e) { k = -1; System.out.println("select");
	 * System.out.println(e.getMessage()); this.close(); } this.close(); return
	 * k; }
	 */
	public ResultSet select(String sql) throws Exception {
		try {
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			rs = stmt.executeQuery(sql);
			// rs = this.getStmtRead().executeQuery(sql);
			return rs;
		} catch (Exception e) {
			System.out.println("select");
			System.out.println(e.getMessage());
		}
		return null;
	}
	/*
	*
	*get current time
	*/
	public String getDate()
	{
		return new java.util.Date().toString();
		
	}
	/*
	 * 解决中文乱码
	 */
	public String chStr(String str) {

		if (str == null) {
			str = "";
		} else {
			try {
				str = (new String(str.getBytes("iso-8859-1"), "gb2312")).trim();
			} catch (Exception e) {
				System.out.println("chStr");
				System.out.println(e.getMessage());
			}
		}
		return str;
	}
	/*
	 * 函数功能:执行无返回值的数据库操作,包括update、delete、add等
	 */
	public void update(String strSQL) throws Exception
	{
		try
		{
			this.getStmtRead().executeUpdate(strSQL);
		}
		catch(SQLException e)
		{
			System.out.println("sql.executeUpdate:" + e.getMessage());
		}
		this.close();
	}
	/*
	 * ?????????????l??
	 */
	public void close() {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception e) {
			System.out.println("rs");
			System.out.println(e.getMessage());
		}
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception e) {
			System.out.println("stmt");
			System.out.println(e.getMessage());
		}
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			System.out.println("conn");
			System.out.println(e.getMessage());
		}
	}
	
	/*
	public static void main(String args[]) throws Exception
	{
		ResultSet sqlRst;
		String strSQL;
		DbConn con = new DbConn();
		
		try
		{
			con.OpenConn();
			
		}
		catch(Exception sqle)
		{
			System.err.println(sqle);
		}
		int i = 1;
		sqlRst = con.select("select * from tbl_xingmuyj");
		if (sqlRst == null)
		{
			System.out.print("目前还没有记录,请登录后台添加!");
		}
		else
		{
			while (sqlRst.next())
			{	
				String test = sqlRst.getString("name");
				System.out.println(test);
			}
		}
	}
	*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -