JAVA学习心得——DBUtil实战:学生信息管理系统雏形

本文分享了使用JAVA的DBUtil工具类实现学生信息管理系统的增删改查操作,详细探讨了如何构建系统的基本功能。
摘要由CSDN通过智能技术生成

使用DBUtil工具类,实现学生信息的增删改查基本功能。

package com.jd.util;

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

/**
 * 数据库工具
 * 
 * @author 冯申然
 */
public class DBUtil {
	
	/**
	 * 程序驱动
	 * 
	 * @author 冯申然
	 */
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 连接数据库
	 * 
	 * @author 冯申然
	 */
	public static  Connection  getconnection() {
		String url = PropertiesUtil.getValue("jdbc.url");
		String userName = PropertiesUtil.getValue("jdbc.userName");
		String root = PropertiesUtil.getValue("jdbc.root");
//		System.out.println(url);
//		System.out.println(userName);
//		System.out.println(root);
		try {
			return  DriverManager.getConnection(url,userName, root);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;	
	}
	
	/**
	 * 数据的增删改查
	 * 
	 * @author 冯申然
	 */
		public static boolean updata(String sql) {//增删改
			Connection connection =null;
			Statement statement =null;
			try {
				connection=getconnection();
				statement = connection.createStatement();
				int result = statement.executeUpdate(sql);
				return result>0;
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(statement,connection);
			}
			return false;
		}
		
		/**
		 * 数据增删改查
		 * @author 冯申然
		 */
		public static boolean updata(String sql,Object...params) {//增删改
			Connection connection =null;
			PreparedStatement prepareStatement=null;
			try {
				connection=getconnection();
				prepareStatement = connection.prepareStatement(sql);
				
				for(int i=1;i<=params.length;i++) {
					prepareStatement.setObject(i, params[i-1]);
				}
				int result = prepareStatement.executeUpdate();
				return result>0;
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(prepareStatement,connection);
			}
			return false;
		}
		
		/**
		 * 登录设置
		 * 
		 * @author 冯申然
		 */
		public static void select(String sql,IRowMapper rowMapper,Object...params) {//登录设置
			ResultSet result =null;
			Statement statement =null;
			Connection connection =null;
			PreparedStatement preparedStatement =null;
			try {
				connection=getconnection();
				preparedStatement =connection.prepareStatement(sql);
				for(int i=1;i<=params.length;i++) {
					preparedStatement.setObject(i, params[i-1]);
				}
				result =preparedStatement.executeQuery();
				rowMapper.rowMapper(result);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(result,statement,connection);
			}
		}
		
		/**
		 * 检验数据是否存在
		 * 
		 * @author 冯申然
		 */
		public static boolean exist (String sql) {//是否存在
			class RowMapper implements IRowMapper{
				boolean state;
				@Override
				public void rowMapper(ResultSet result) {
					try {
						state=result.next();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}		
			}
			RowMapper rowMapper =new RowMapper();
			select(sql,rowMapper);
			return rowMapper.state;
		}
		
		/**
		 * 检验数据是否存在
		 * 
		 * @author 冯申然
		 */
		public static boolean exist (String sql,Object...params) {//是否存在
			class RowMapper implements IRowMapper{
				boolean state;
				@Override
				public void rowMapper(ResultSet result) {
					try {
						state=result.next();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}		
			}
			RowMapper rowMapper =new RowMapper();
			select(sql,rowMapper,params);
			return rowMapper.state;
		}
		/**
		 * 批量操作
		 * 
		 * @author 冯申然
		 */
		public static boolean batch(String ... sql) {
			Connection connection =null;
			Statement statement =null;
			try {
				connection = getconnection();
				connection.setAutoCommit(false);
				statement =connection.createStatement();
				for (String string : sql) {
					statement.addBatch(string);
				}
				statement.executeBatch();
				connection.setAutoCommit(true);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				if (connection!=null) {
					try {
						connection.rollback();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			} finally {
				if (statement!=null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} 
				}
				if (connection!=null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} 
				}
			}
			return false;
		}
		
		/**
		 * 查询数据
		 * 
		 * @author 冯申然
		 */
		public static void select(String sql,IRowMapper rowMapper) {//查询
				Connection connection =null;
				Statement statement =null;
				ResultSet result =null;
				try {
					connection=getconnection();
					statement = connection.createStatement();
					result = statement.executeQuery(sql);
					rowMapper.rowMapper(result);
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					close(result,statement,connection);
				}	
			}
		
		/**
		 * 查询数据
		 * 
		 * @author 冯申然
		 */
		public static void Select(String sql,IRowMapper rowMapper,Object...params) {//查询
			Connection connection =null;
			Statement statement =null;
			ResultSet result =null;
			PreparedStatement preparedStatement =null;
			try {
				connection=getconnection();
				preparedStatement =connection.prepareStatement(sql);
				for(int i=1;i<=params.length;i++) {
					preparedStatement.setObject(i, params[i-1]);
				}
				result =preparedStatement.executeQuery() ;
				rowMapper.rowMapper(result);
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				close(result,statement,connection);
			}	
		}
		/**
		 * 批量处理
		 * 
		 * @author 冯申然
		 */
		public static boolean batch(String sql,Object[]...parmas) {
			Connection connection =null;
			PreparedStatement prepareStatement =null;
			try {
				connection = getconnection();
				connection.setAutoCommit(false);
				prepareStatement =connection.prepareStatement(sql);
				for (int i=0;i<parmas.length;i++) {
					for(int j=1;j<=parmas[i].length;i++){
						prepareStatement.setObject(j, parmas[i][j-1]);
					}
					prepareStatement.addBatch();
				}
				prepareStatement.executeBatch();
				connection.setAutoCommit(true);
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				close(prepareStatement,connection);
			}
			return false;
		}
		
		/**
		 * 释放资源
		 * 
		 * @author 冯申然
		 */
		private static void close(Statement statement,Connection connection){
				
			if (statement!=null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
			}
			if (connection!=null) {
				try {
					connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}	
				}
			}
		/**
		 * 释放资源
		 * 
		 * @author 冯申然
		 */
			private static void close(ResultSet result,Statement statement,Connection connection){
				if (result!=null) {
					try {
						result.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (statement!=null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} 
				}
				if (connection!=null) {
						try {
							connection.close();
						} catch (SQLException e) {
							e.printStackTrace();
						} 		
				}
			}
}


package com.parddu.dao; import java.io.IOException; import java.sql.*; import java.util.Properties; /** * 数据库功能类 * @author parddu * @version Sep 29, 2010 9:49:31 AM */ class DButil { private String driver=null; //驱动 private String dbName=null; //数据库名 private String host=null; //主机名 private String point=null; //端口 private String userName=null; //登录帐号 private String userPass=null; //登录密码 private static DButil info = null; private DButil(){} /** * 初始化方法,加载数据库连接信息 * @throws IOException */ private static void init() throws IOException{ Properties prop = new Properties(); prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop.getProperty("host"); info.point = prop.getProperty("point"); info.userName = prop.getProperty("userName"); info.userPass = prop.getProperty("userPass"); } /** * 得到数据库连接对象 * @return 数据库连接对象 */ static Connection getConn(){ Connection conn=null; if(info == null){ try { init(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } if(info!=null){ try { Class.forName(info.driver); String url="jdbc:sqlserver://" + info.host + ":" + info.point + ";databaseName=" + info.dbName; conn=DriverManager.getConnection(url,info.userName,info.userPass); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } else{ throw new RuntimeException("读取数据库配置信息异常!"); } return conn; } /** * 关闭查询数据库访问对象 * @param rs 结果集 * @param st 上下文 * @param conn 连接对象 */ static void closeConn(ResultSet rs, Statement st,Connection conn){ try { rs.close(); } catch (Exception e) {} try { st.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e) {} } /** * 关闭增、删、该数据库访问对象 * @param st 上下文对象 * @param conn 连接对象 */ static void closeConn(Statement st ,Connection conn){ try{ st.close(); conn.close(); }catch(Exception e){} } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值