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

来源:互联网 发布:农村淘宝合伙人退出潮 编辑:程序博客网 时间:2024/05/17 03:51

在没有配置环境的条件下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=rootjdbc.password=rootjdbc.driver=com.mysql.jdbc.Driverjdbc.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();}}}}


0 0