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

📄 eljdbc.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
字号:
package com.elan.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ElJDBC {
	private static String dbUrl = "";
	private static String password = "";
	private static String username = "";
	/*private static ElJDBC elJDBC = null;
	static {
		elJDBC = new ElJDBC();
	}*/
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.jdbc.mysql.Driver");
			conn = DriverManager.getConnection(dbUrl);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			closeConn(conn);
		} catch (SQLException e) {
			e.printStackTrace();
			closeConn(conn);
		}
		return conn;
	}

	public static Statement getStatement(String sql, Connection conn) {
		Statement stmt = null;
		if(conn != null && sql != null && "".equals(sql)) {
			try {
				stmt = conn.createStatement();
			} catch (SQLException e) {
				e.printStackTrace();
				closeStmt(stmt);
				closeConn(conn);
			}
		}
		return stmt;
	}

	public static ResultSet getResultSet(Statement stmt) {
		if(stmt != null) {
	//		stmt.e
		}
		return null;
	}
	
	public static ResultSet executeQuery(String sql, Statement stmt) {
		ResultSet rs = null;
		if(stmt != null) {
			try {
				rs = stmt.executeQuery(sql);
			} catch (SQLException e) {
				e.printStackTrace();
				closeStmt(stmt);
				try {
					closeConn(stmt.getConnection());
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
		return rs;
	}
	
	public static void closeConn(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void closeStmt(Statement stmt) {
		if(stmt != null) {
			try {
				stmt.close();
				stmt = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void closeRs(ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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