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

📄 dbutil.java

📁 开发的一个java web的任务管理系统。
💻 JAVA
字号:
package unicom.util;

import java.io.*;
import java.sql.*;
import java.util.*;

/**
 * @author xiaogang
 * 日期:Apr 17, 2008
 * 功能:封装数据库的基本增、删、改、查操作
 * 优点:可移植性好
 * 缺点:
 * 建议:
 */
public class DBUtil {
	
	private static String driverName = "";
	private static String url = "";
	private static String user = "";
	private static String password = "";
	
	//通过静态块读取配置文件的内容到属性中
	static
	{
		try{
			InputStream is = DBUtil.class.getResourceAsStream("/database.properties");
			//InputStream is = new FileInputStream("c:\\database.properties");
			Properties props = new Properties();
			props.load(is); 
			is.close();
			driverName = props.getProperty("driverName");
			url = props.getProperty("url");
			user = props.getProperty("user");
			password = props.getProperty("password");
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}
	public DBUtil() {

	}
	
	//取得数据库连接
	public static Connection getConn()
	{
		Connection conn = null;
		try {
			try {
				Class.forName(driverName);
				conn = DriverManager.getConnection(url,user,password);
			} catch (ClassNotFoundException e) {
				System.out.println(e);
			}
			
		} catch (SQLException e) {
			System.out.println(e);
		}
		return conn;
		
	}
	//取得Statement对象
	public static Statement getStatement(Connection conn) {
		Statement stmt = null; 
		try {
			if(conn != null) {
				stmt = conn.createStatement();
			}
		} catch (SQLException e) {
			System.out.println(e);
		}
		return stmt;
	}
	
//	取得PreparedStatement对象
	public static PreparedStatement prepare(Connection conn,  String sql) {
		PreparedStatement pstmt = null; 
		try {
			if(conn != null) {
				pstmt = conn.prepareStatement(sql);
			}
		} catch (SQLException e) {
			System.out.println(e);
		}
		return pstmt;
	}
	
	//执行insert,update,delete操作
	public static void executeUpdate(Statement stmt, String sql) {
		try {
			if(stmt != null) {
				stmt.executeUpdate(sql);
			}
		} catch (SQLException e) {
			
			System.out.println(e);
		}
	}
	
	public static void executeUpdate(PreparedStatement pstmt) {
		try {
			if(pstmt != null) {
				pstmt.executeUpdate();
			}
		} catch (SQLException e) {
			
			System.out.println(e);
		}
	}
	//通过Statement和sql取得ResultSet对象
	public static ResultSet getResultSet(Statement stmt, String sql) {
		ResultSet rs = null;
		try {
			if(stmt != null) {
				rs = stmt.executeQuery(sql);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static ResultSet getResultSet(PreparedStatement pstmt) {
		ResultSet rs = null;
		try {
			if(pstmt != null) {
				rs = pstmt.executeQuery();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	//关闭资源
	public static void close(Connection conn) {
		try {
			if(conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(Statement stmt) {
		try {
			if(stmt != null) {
				stmt.close();
				stmt = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void close(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
				rs = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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