zoukankan      html  css  js  c++  java
  • java(JDBC连接数据库)[对PreparedStatement进行封装]

    package com.iflytec.ex01;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class ConnectionDB {
    
    	/**
    	 * 数据库驱动类名称
    	 */
    	private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    
    	/**
    	 * 连接字符串
    	 */
    	private static final String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=Northwind";
    
    	/**
    	 * 用户名
    	 */
    	private static final String USERNAME = "sa";
    
    	/**
    	 * 密码
    	 */
    	private static final String USERPASSWORD = "111111";
    
    	/**
    	 * 创建数据库连接类
    	 */
    	private Connection connnection = null;
    
    	private PreparedStatement preparedStatement = null;
    
    	/**
    	 * 创建结果集对象
    	 */
    	private ResultSet resultSet = null;
    
    	static {
    		try {
    			// 加载数据库驱动程序
    			Class.forName(DRIVER);
    		} catch (ClassNotFoundException e) {
    			System.out.println("加载驱动错误");
    			System.out.println(e.getMessage());
    		}
    	}
    
    	/**
    	 * 建立数据库连接
    	 * 
    	 * @return
    	 */
    	public Connection getConnection() {
    		try {
    			// 获取连接
    			connnection = DriverManager.getConnection(URLSTR, USERNAME,
    					USERPASSWORD);
    		} catch (SQLException e) {
    			System.out.println(e.getMessage());
    		}
    		return connnection;
    	}
    
    	public int executeUpdate(String sql, Object[] params) {
    		int affectedLine = 0;
    		try {
    			connnection = this.getConnection();
    			preparedStatement = connnection.prepareStatement(sql);
    			for (int i = 0; i < params.length; i++) {
    				preparedStatement.setObject(i + 1, params[i]);
    			}
    
    			affectedLine = preparedStatement.executeUpdate();
    
    		} catch (SQLException e) {
    			System.out.println(e.getMessage());
    		} finally {
    			closeAll();
    		}
    		return affectedLine;
    	}
    	
    
    	private ResultSet executeQueryRS(String sql, Object[] params) {
    		try {
    			connnection = this.getConnection();
    			preparedStatement = connnection.prepareStatement(sql);
    			for (int i = 0; i < params.length; i++) {
    				preparedStatement.setObject(i + 1, params[i]);
    			}
    
    			resultSet = preparedStatement.executeQuery();
    
    		} catch (SQLException e) {
    			System.out.println(e.getMessage());
    		} 
    		
    		return resultSet;
    	}
    	
    	/**
    	 * 获取结果集,并将结果放在List中
    	 * @param sql SQL语句
    	 * @return List结果集
    	 */
    	public List<Object> excuteQuery(String sql, Object[] params) { 
    		ResultSet rs = executeQueryRS(sql,params);
    		ResultSetMetaData rsmd = null;
    		int columnCount = 0;
    		try {
    			rsmd = rs.getMetaData();
    			columnCount = rsmd.getColumnCount();
    		} catch (SQLException e1) {
    			System.out.println(e1.getMessage());
    		}
    		
    		List<Object> list = new ArrayList<Object>();
    		
    		try {
    			while(rs.next()) {
    				Map<String, Object> map = new HashMap<String, Object>();
    				for(int i = 1; i <= columnCount; i++) {
    					map.put(rsmd.getColumnLabel(i), rs.getObject(i));				
    				}
    				list.add(map);
    			}
    		} catch (SQLException e) {
    			System.out.println(e.getMessage());
    		} finally {
    			closeAll();
    		}
    		
    		return list;
    	}
    
    	private void closeAll() {
    
    		if (resultSet != null) {
    			try {
    				resultSet.close();
    			} catch (SQLException e) {
    				System.out.println(e.getMessage());
    			}
    		}
    
    		if (preparedStatement != null) {
    			try {
    				preparedStatement.close();
    			} catch (SQLException e) {
    				System.out.println(e.getMessage());
    			}
    		}
    		
    		if (connnection != null) {
    			try {
    				connnection.close();
    			} catch (SQLException e) {
    				System.out.println(e.getMessage());
    			}
    		}
    	}
    }
    

  • 相关阅读:
    小米面试之股票收益最大
    第三周:同时管理64位和32位版本的Python,并用Pyinstaller打包成exe
    第三周 anaconda的安装
    第二周:python实现线性回归(哑变量回归)的高效方法
    第一周:在python里调用C文件中函数
    13G:神奇的数列
    13F:图像分割
    13E:吃奶酪
    13D:拖延症
    13B:回文子串
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671578.html
Copyright © 2011-2022 走看看