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

📄 dbconnection.java

📁 用jsp+servlet
💻 JAVA
字号:


import java.sql.*;

//==========================================
// Define Class DBConnection
//==========================================
public class DBConnection {
	public String sql_driver = "org.apache.derby.jdbc.ClientDriver";

	public String sql_url = "jdbc:derby://localhost:1527";

	public String sql_DBName = "atm";

	public String user = "xx";

	public String pwd = "xx";

	Connection conn = null;

	Statement stmt = null;

	public ResultSet rs = null;

	public boolean setDriver(String drv) {
		this.sql_driver = drv;
		return true;
	}

	public String getDriver() {
		return this.sql_driver;
	}

	public boolean setUrl(String url) {
		this.sql_url = url;
		return true;
	}

	public boolean setDBName(String dbname) {
		this.sql_DBName = dbname;
		return true;
	}

	public String getDBName() {
		return this.sql_DBName;
	}

	public boolean setUser(String user) {
		this.user = user;
		return true;
	}

	public String getUser() {
		return this.user;
	}

	public boolean setPwd(String pwd) {
		this.pwd = pwd;
		return true;
	}

	public String getPwd() {
		return this.pwd;
	}

	public DBConnection() {
		try {
			Class.forName(sql_driver);// 加载数据库驱动程序
			this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName
					+ ";create=true");
			this.stmt = this.conn.createStatement();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	// 执行查询操作
	public ResultSet executeQuery(String strSql) {
		try {
			this.rs = stmt.executeQuery(strSql);
			return this.rs;
		} catch (SQLException e) {
			System.out.println(e.toString());
			return null;
		} catch (NullPointerException e) {
			System.out.println(e.toString());
			return null;
		}
	}

	// 执行数据的插入、删除、修改操作
	public boolean execute(String strSql) {
		try {
			if (this.stmt.executeUpdate(strSql) == 0)
				return false;
			else
				return true;
		} catch (SQLException e) {
			System.out.println(e.toString());
			return false;
		} catch (NullPointerException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	// 结果集指针跳转到某一行
	public boolean rs_absolute(int row) {
		try {
			this.rs.absolute(row);
			return true;
		} catch (SQLException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	public void rs_afterLast() {
		try {
			this.rs.afterLast();
		} catch (SQLException e) {
			System.out.println(e.toString());
		}
	}

	public void rs_beforeFirst() {
		try {
			this.rs.beforeFirst();
		} catch (SQLException e) {
			System.out.print(e.toString());
		}
	}

	public void rs_close() {
		try {
			this.rs.close();
		} catch (SQLException e) {
			System.out.print(e.toString());
		}
	}

	public void rs_deleteRow() {
		try {
			this.rs.deleteRow();
		} catch (SQLException e) {
			System.out.print(e.toString());
		}
	}

	public boolean rs_first() {
		try {
			this.rs.first();
			return true;
		} catch (SQLException e) {
			System.out.print(e.toString());
			return false;
		}
	}

	public Double rs_getDouble(String column) {
		try {
			return this.rs.getDouble(column);
		} catch (SQLException e) {
			System.out.println(e.toString());
			return null;
		}
	}

	public String rs_getString(String column) {
		try {
			return this.rs.getString(column);
		} catch (SQLException e) {
			System.out.println(e.toString());
			return null;
		}
	}

	// 此方法用于获取大段文本,
	// 将其中的回车换行替换为<br>
	// 输出到html页面
	public String rs_getHtmlString(String column) {
		try {
			String str1 = this.rs.getString(column);
			String str2 = "\r\n";
			String str3 = "<br>";
			return replaceAll(str1, str2, str3);
		} catch (SQLException e) {
			System.out.println(e.toString());
			return null;
		}
	}

	// 把str1字符串中的str2字符串替换为str3字符串
	private static String replaceAll(String str1, String str2, String str3) {
		StringBuffer strBuf = new StringBuffer(str1);
		int index = 0;
		while (str1.indexOf(str2, index) != -1) {
			index = str1.indexOf(str2, index);
			strBuf.replace(str1.indexOf(str2, index), str1.indexOf(str2, index)
					+ str2.length(), str3);
			index = index + str3.length();

			str1 = strBuf.toString();
		}
		return strBuf.toString();
	}

	public int rs_getInt(String column) {
		try {
			return this.rs.getInt(column);
		} catch (SQLException e) {
			System.out.println(e.toString());
			return -1;
		}
	}

	public int rs_getInt(int column) {
		try {
			return this.rs.getInt(column);
		} catch (SQLException e) {
			System.out.println(e.toString());
			return -1;
		}
	}

	public boolean rs_next() {
		try {
			return this.rs.next();
		} catch (SQLException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	// 判断结果集中是否有数据
	public boolean hasData() {
		try {
			boolean has_Data = this.rs.first();
			this.rs.beforeFirst();
			return has_Data;
		} catch (SQLException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	public boolean rs_last() {
		try {
			return this.rs.last();
		} catch (SQLException e) {
			System.out.println(e.toString());
			return false;
		}
	}

	public boolean rs_previous() {
		try {
			return this.rs.previous();
		} catch (Exception e) {
			System.out.println(e.toString());
			return false;
		}
	}

	// main方法,调试用
	public static void main(String args[]) {
		try {
			DBConnection myconn = new DBConnection();
			myconn
					.execute("create table atm ( accountnumber int,pin int, lastname varchar(50), firstname varchar(50) ,balanceamount double)");
			// myconn.setDBName("shopping");
			// myconn.DBConn();
			myconn
					.execute("Insert Into atm (accountnumber,pin,lastname,firstname,balanceamount) values (001,1111,'xx','xxx',0.1)");
			// myconn.execute("Update tbl_test set name='yyyyyyyyyyyy' where
			// id=10");
			// myconn.execute("Delete from tbl_test where id=1");

			// boolean hasData = myconn.hasData();
			// System.out.println("has data:" + hasData);
			// rs.first();

		} catch (Exception e) {
			System.err.println(e.toString());
		}
	}
}

⌨️ 快捷键说明

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