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

📄 dbconnect.java

📁 文件的上传和下载的实现,用JAva编写,非常简单
💻 JAVA
字号:
package edu.whut.cwts.datasource;

import java.sql.*;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBConnect {

	DataSource ds = null;
	
	Context ctx = null;
	
	Connection conn = null;

	ResultSet rs = null;

	Statement stmt = null;

	/**
	 * @return 返回 conn。
	 */
	public Connection getConn() {
		return conn;
	}

	/**
	 * @param conn
	 *            要设置的 conn。
	 */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	/**
	 * @return 返回 rs。
	 */
	public ResultSet getRs() {
		return rs;
	}

	/**
	 * @param rs
	 *            要设置的 rs。
	 */
	public void setRs(ResultSet rs) {
		this.rs = rs;
	}

	/**
	 * @return 返回 stmt。
	 */
	public Statement getStmt() {
		return stmt;
	}

	/**
	 * @param stmt
	 *            要设置的 stmt。
	 */
	public void setStmt(Statement stmt) {
		this.stmt = stmt;
	}
	
	// 构造函数
	public DBConnect() {

		Hashtable ht = new Hashtable();
		ht.put(Context.INITIAL_CONTEXT_FACTORY,	"weblogic.jndi.WLInitialContextFactory");
		ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
		try {
			ctx = new InitialContext(ht);
			ds = (DataSource) ctx.lookup("hyfdbjndi");
			conn = ds.getConnection("weblogic", "12341234");// 此处是WebLogic8.1的域用户和密码
			stmt = conn.createStatement();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public ResultSet executeQuery(String sql) throws SQLException {

		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException ex) {
			System.out.println("执行SQL语句出错: " + ex.getMessage());
		}
		return rs;
	}

	public int executeUpdate(String sql) throws SQLException {
		int flag = 0;
		try {
			flag = stmt.executeUpdate(sql);
		} catch (SQLException ex) {
			System.err.println("执行SQL语句出错: " + ex.getMessage());
		}
		return flag;
	}

	public boolean executeDelete(String sql) {
		boolean flag = true;
		try {
			flag = stmt.execute(sql);
		} catch (SQLException ex) {
			System.err.println("执行SQL语句出错: " + ex.getMessage());
		}
		return flag;
	}

	public boolean executeInsert(String sql) throws SQLException {
		boolean flag = true;
		try {
			flag = stmt.execute(sql);
		} catch (SQLException ex) {
			System.err.println("执行SQL语句出错: " + ex.getMessage());
		}
		return flag;
	}

	// 用于构造模糊查询语句
	public String selectStr(String str) {
		if (str.equals("")) {
			str = "LIKE'%'";
		} else {
			str = ("LIKE'%" + str + "%'");
		}
		return str;
	}

	public Connection getConnection() {
		return conn;
	}

	public boolean close() {
		try {
			if (this.rs != null) {
				this.rs.close();
				this.rs = null;
			}
			if (stmt != null) {
				this.stmt.close();
				this.stmt = null;
			}
			if (conn != null) {
				this.conn.close();
				this.conn = null;
			}
			return true;
		} catch (Exception err) {
			return false;
		}
	}

	public static void main(String[] args) {

		String sql = "select * from zdxxb where zddm like'%0000' ";

		DBConnect dbconn = new DBConnect();

		try {

			ResultSet rs = dbconn.executeQuery(sql);
			// i= dbconn.executeUpdate(sql);
			while (rs.next()) {
				System.out.println(rs.getString(1) + "    " + rs.getString(2)
						+ "   ");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dbconn.close();

		// System.out.println(i);
		// System.out.println(rs2);
		// if(i==1&&rs2==false){result=true;}
		// System.out.println(result);
	}

}

⌨️ 快捷键说明

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