jdbc 读取属性文件

xiaoxiao2021-02-27  313

1.首先编写  jdbc的属性文件  db.properties

# 数据库的连接信息:驱动类Driver,连接地址url,用户名:username,密码:password jdbc.Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.url=jdbc:sqlserver://127.0.0.1:1433;databaseName=MyDB; jdbc.username=sa jdbc.password=sasa 2.编写访问数据库所使用的的Dao,封装好一个类里面有查询方法和相应的DML方法,避免代码冗余(每个方法都加载一遍驱动) 相应的Dao代码如下: public class Dao { private static String driver; private static String url; private static String username; private static String password; //静态代码块读取属性文件并赋值:静态代码块的作用,程序运行只加载一次 静态代码块中读取静态属性 static { //读取属性文件 ResourceBundle rb= PropertyResourceBundle.getBundle("config/mybatis/db"); //赋值 Dao.driver=rb.getString("jdbc.driver"); Dao.url=rb.getString("jdbc.url"); Dao.username=rb.getString("jdbc.username"); Dao.password=rb.getString("jdbc.password"); } //查询 不定长参数必须在参数的最后 public List<Map<String,Object>> doSelect(String sql,Object...params) { List<Map<String,Object>> list=new ArrayList<Map<String, Object>>(); //声明JDBC对象 Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; try { //加载驱动 Class.forName(driver); //建立连接 conn= DriverManager.getConnection(url,username,password); //创建prepareStatement对象 pstmt=conn.prepareStatement(sql); //给参数赋值 for(int i=0;i<params.length;i++) { pstmt.setObject(i+1,params[i]); } //执行sql语句并返回结果集 rs=pstmt.executeQuery(); //得到结构对象 ResultSetMetaData rsmd=rs.getMetaData(); //得到列数 int count=rsmd.getColumnCount(); while (rs.next()) { //生成Map用来存放每一行 Map<String,Object> map=new HashMap<String, Object>(); //遍历每一列 根据列名得到所对应的值 for (int columnIndex=1;columnIndex<=count;columnIndex++) { //得到列名 String columnName=rsmd.getColumnName(columnIndex); //根据列名得到值 Object value=rs.getObject(columnName); map.put(columnName,value); } //放到List list.add(map); } }catch (Exception e) { e.printStackTrace(); } finally { try { if(rs!=null) { rs.close(); } if (pstmt!=null) { pstmt.close(); } if(conn!=null) { conn.close(); } }catch (Exception e) { e.printStackTrace(); } } return list; } //doDML public int doDML(String sql,Object...params) { int result=0; Connection conn=null; PreparedStatement pstmt=null; try { //加载驱动 Class.forName(driver); //建立连接 conn=DriverManager.getConnection(url,username,password); //得到pstmt pstmt=conn.prepareStatement(sql); //赋值 for(int i=0;i<params.length;i++) { pstmt.setObject(i+1,params[i]); } //执行sql result =pstmt.executeUpdate(); }catch (Exception e) { e.printStackTrace(); } finally { try { if(pstmt!=null) { pstmt.close(); } if (conn!=null) { conn.close(); } }catch (Exception e) { e.printStackTrace(); } } return result; } }编写一个测试类来看看程序是否能顺利运行吧public class Test { public static void main(String[] args) { Dao dao=new Dao(); String sql="insert into users values(?,?,?,?)"; String uuid= UUID.randomUUID().toString().replace("-",""); int result= dao.doDML(sql,uuid,"张三","123","男"); System.out.print(result); }这时右键的run Test.java 你会发现报一个错 classNotFoundException 无法找到该类 那是因为你没有导包啊 idea的导包:1) 在WEB-INF下新建文件夹lib 将所需的jar包复制到里面 然后在工程里进行相应的导入 eclipse直接进行build to path idea 进行包导入的方法1)进入到project structure --->libraries--->点击“+”--->java 选择刚才复制到项目的lib文件夹,这样就把lib文件夹下的包导入到项目里了。 成功运行  
转载请注明原文地址: https://www.6miu.com/read-3902.html

最新回复(0)