JDBC原理:不用配置文件和配置文件下的对比

在没有配置环境的条件下JdbcUtil.java

public class JDBCUtil {
	//表示定义数据库的用户名
	private final String USERNAME="root";
	//定义数据库的密码
	private final String PASSWORD="root";
	//定义数据库的驱动信息
	private final String  DRIVER="com.mysql.jdbc.Driver";
	//定义访问数据库到的地址
	private final String URL="jdbc:mysql://localhost:3306/studentpage";
	//定义数据库的连接
	private Connection conn;
	//定义sqk语句的执行对象
	private PreparedStatement pstmt;
	//定义查询返回的结果集
	private ResultSet resultSet;
	public JDBCUtil(){
		
	}
	/**
	 * 获取数据库连接对象
	 */
	public Connection getConnection(){
		try{
			Class.forName(DRIVER);//注册驱动
			conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
		}catch(Exception e){
			throw new RuntimeException("get connection error!");
		}
		return conn;
	}
	//执行更新操作
	public boolean updateByPreparedStatement(String sql,List<?> params)
		throws SQLException{
		boolean flag=false;
		int result=-1;//表示当前用户执行添加删除和修改的时候所影响的数据行数
		pstmt =conn.prepareStatement(sql);
		int index=1;
		//填充sql语句中的占位符
		if(params!=null&&!params.isEmpty()){
			for(int i=0;i<params.size();i++){
				pstmt.setObject(index++,params.get(i));
			}
		}
		result=pstmt.executeUpdate();
		flag=result>0?true:false;
		return flag;
	}
	//执行查询操作
	public List<Map<String,Object>>findResult(String sql,List<?> params)
		throws SQLException{
		List<Map<String,Object>>list =new ArrayList<Map<String,Object>>();
		int index=1;
		pstmt=conn.prepareStatement(sql);
		if(params!=null&&!params.isEmpty()){
			for(int i=0;i<params.size();i++){
				pstmt.setObject(index++,params.get(i));
			}
		}
		resultSet=pstmt.executeQuery();
		ResultSetMetaData metaData=resultSet.getMetaData();
		int cols_len=metaData.getColumnCount();
		while(resultSet.next()){
			Map<String,Object>map=new HashMap<String,Object>();
			for(int i=0;i<cols_len;i++){
				String cols_name=metaData.getColumnName(i+1);
				Object cols_value=resultSet.getObject(cols_name);
				if(cols_value==null){
					cols_value="";
				}
				map.put(cols_name, cols_value);
			}
			list.add(map);
			
		}
		return list;
	}
	/**
	 * 释放资源
	 */
	public void releaseConn(){
		if(resultSet!=null){
			try{
				resultSet.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try{
				pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
在配置文件下的JdbcUtils.java

配置文件:

jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/studentpage
JdbcUtils.java
public class JDBCUtil {
	//表示定义数据库的用户名
	private static String USERNAME;
	//定义数据库的密码
	private static String PASSWORD;
	//定义数据库的驱动信息
	private static String  DRIVER;
	//定义访问数据库到的地址
	private static String URL;
	//定义数据库的连接
	private Connection conn;
	//定义sqk语句的执行对象
	private PreparedStatement pstmt;
	//定义查询返回的结果集
	private ResultSet resultSet;
	static{
		loadConfig();
	}
	/**
	 * 加载数据库配置信息,并给相关的属性赋值
	 */
	public static void loadConfig(){
		try {
			InputStream inStream=JDBCUtil.class.getResourceAsStream("/jdbc.properties");
			Properties prop=new Properties();
			prop.load(inStream);
			USERNAME=prop.getProperty("jdbc.username");
			PASSWORD=prop.getProperty("jdbc.password");
			DRIVER=prop.getProperty("jdbc.driver");
			URL=prop.getProperty("jdbc.url");
		} catch (Exception e) {
			throw new RuntimeException("读取数据库配置文件异常!",e);
		}
	}
	
	public JDBCUtil(){
		
	}
	/**
	 * 获取数据库连接对象
	 */
	public Connection getConnection(){
		try{
			Class.forName(DRIVER);//注册驱动
			conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
		}catch(Exception e){
			throw new RuntimeException("get connection error!");
		}
		return conn;
	}
	//执行更新操作
	public boolean updateByPreparedStatement(String sql,List<?> params)
		throws SQLException{
		boolean flag=false;
		int result=-1;//表示当前用户执行添加删除和修改的时候所影响的数据行数
		pstmt =conn.prepareStatement(sql);
		int index=1;
		//填充sql语句中的占位符
		if(params!=null&&!params.isEmpty()){
			for(int i=0;i<params.size();i++){
				pstmt.setObject(index++,params.get(i));
			}
		}
		result=pstmt.executeUpdate();
		flag=result>0?true:false;
		return flag;
	}
	//执行查询操作
	public List<Map<String,Object>>findResult(String sql,List<?> params)
		throws SQLException{
		List<Map<String,Object>>list =new ArrayList<Map<String,Object>>();
		int index=1;
		pstmt=conn.prepareStatement(sql);
		if(params!=null&&!params.isEmpty()){
			for(int i=0;i<params.size();i++){
				pstmt.setObject(index++,params.get(i));
			}
		}
		resultSet=pstmt.executeQuery();
		ResultSetMetaData metaData=resultSet.getMetaData();
		int cols_len=metaData.getColumnCount();
		while(resultSet.next()){
			Map<String,Object>map=new HashMap<String,Object>();
			for(int i=0;i<cols_len;i++){
				String cols_name=metaData.getColumnName(i+1);
				Object cols_value=resultSet.getObject(cols_name);
				if(cols_value==null){
					cols_value="";
				}
				map.put(cols_name, cols_value);
			}
			list.add(map);
			
		}
		return list;
	}
	/**
	 * 释放资源
	 */
	public void releaseConn(){
		if(resultSet!=null){
			try{
				resultSet.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try{
				pstmt.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未名胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值