第一步:在src下面建一个包com.xsl.conf
第二步:在建好的包下面新建一个jdbc.properties
第三步:在jdbc.properties里写入内容如下:
driver = oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@127.0.0.1:1521:orcl或jdbc:oracle:thin:@localhost:1521:orcl
uname = scott
upwd = tiger
第四步:建BaseDao
1 package com.xsl.dao; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.lang.reflect.Field; 6 import java.lang.reflect.Method; 7 import java.sql.Connection; 8 import java.sql.Date; 9 import java.sql.DriverManager; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 import java.sql.SQLException; 13 import java.sql.Statement; 14 import java.util.ArrayList; 15 import java.util.List; 16 import java.util.Properties; 17 18 import com.xsl.entity.Student; 19 20 public class BaseDao { 21 22 private static Properties ps = new Properties(); 23 24 static{ 25 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("com/xsl/conf/jdbc.properties"); 26 try { 27 ps.load(is); 28 System.out.println(ps); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 /** 35 * 获得数据库连接 36 */ 37 public static Connection getConnection(){ 38 Connection conn = null; 39 try { 40 Class.forName(ps.getProperty("driver")); //"oracle.jdbc.driver.OracleDriver" 41 conn = DriverManager.getConnection(ps.getProperty("url"),ps.getProperty("uname"),ps.getProperty("upwd")); //"jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger"或//"jdbc:oracle:thin:@localhost:orcl","scott","tiger" 42 } catch (ClassNotFoundException e) { 43 e.printStackTrace(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 return conn; 48 } 49 50 /** 51 * 添加 52 * @param obj:需要被保存的对象 53 * @return 受影响的行数 54 */ 55 public static int save(Object obj){ 56 //insert into 表名(列名1,列名2...) values(?,?...) 57 int row = 0; 58 //获取需要被保存实体对象的Class类型的对象 59 Class c = obj.getClass(); 60 //获取实体类中的属性对象 61 Field [] fs = c.getDeclaredFields(); 62 //给属性对象解锁 63 Field.setAccessible(fs, true); 64 //拼SQL语句 65 StringBuffer sql = new StringBuffer(); 66 sql.append(" insert into "); 67 sql.append(c.getSimpleName());//添加表名 68 sql.append(" ( "); 69 for(int i=1;i<fs.length;i++){//添加列名 70 sql.append(fs[i].getName()); 71 if(i<fs.length-1){//判断没有到最后一个列 72 sql.append(" , "); 73 } 74 } 75 sql.append(" ) "); 76 sql.append(" values ("); 77 for(int i=1;i<fs.length;i++){ 78 sql.append(" ? "); 79 if(i<fs.length-1){ 80 sql.append(" , "); 81 } 82 } 83 sql.append(" ) "); 84 System.out.println(sql); 85 Connection conn = getConnection(); 86 PreparedStatement pstmt = null; 87 try { 88 pstmt = conn.prepareStatement(sql.toString()); 89 //给占位符赋值 90 for(int i=1;i<fs.length;i++){ 91 //stu.getName();stu.getMoney(); 92 //fs[i].get(obj)从obj这个对象中获取fs[i]这个属性对应的值 93 pstmt.setObject(i, fs[i].get(obj)); 94 } 95 row = pstmt.executeUpdate(); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 }finally{ 99 closeAll(conn, pstmt, null); 100 } 101 return row; 102 } 103 104 /** 105 * 查询 106 */ 107 public static List queryList(Class c){ 108 List list = new ArrayList(); 109 Field [] fs = c.getDeclaredFields(); 110 Field.setAccessible(fs, true); 111 //select * from 表名 112 StringBuffer sql = new StringBuffer(); 113 sql.append(" select * from "); 114 sql.append(c.getSimpleName()); 115 Connection conn = getConnection(); 116 PreparedStatement pstmt = null; 117 ResultSet rs = null; 118 try { 119 pstmt = conn.prepareStatement(sql.toString()); 120 rs = pstmt.executeQuery(); 121 while(rs.next()){ 122 //Student stu = new Student(); 123 //stu.setName(rs.getString("name")); 124 //stu.setMoney(rs.getDouble("money")); 125 Object obj = c.newInstance(); 126 for(int i=0;i<fs.length;i++){ 127 //fs[i].get(obj);stu.getName(); 128 //fs[i].getName()获取属性名 129 //rs.getObject(Objet obj)在结果集中按列名取值 130 //fs[i].set(obj,value);调用属性的赋值方法,给属性赋值 131 fs[i].set(obj, rs.getObject(fs[i].getName()));//stu.setName(rs.getString("name")); 132 } 133 list.add(obj); 134 } 135 } catch (Exception e) { 136 e.printStackTrace(); 137 }finally{ 138 closeAll(conn, pstmt, rs); 139 } 140 return list; 141 } 142 143 /** 144 * 删除 145 */ 146 public static int delete(Object obj){ 147 int row = 0; 148 //delete from 表名 where 主键=? 149 Class c = obj.getClass(); 150 Field [] fs = c.getDeclaredFields(); 151 Field.setAccessible(fs, true); 152 StringBuffer sql = new StringBuffer(); 153 sql.append(" delete from "); 154 sql.append(c.getSimpleName()); 155 sql.append(" where "); 156 sql.append(fs[0].getName()); 157 sql.append(" = ?"); 158 //System.out.println(sql); 159 Connection conn = getConnection(); 160 PreparedStatement pstmt = null; 161 ResultSet rs = null; 162 try { 163 pstmt = conn.prepareStatement(sql.toString()); 164 pstmt.setObject(1, fs[0].get(obj)); 165 row = pstmt.executeUpdate(); 166 } catch (Exception e) { 167 e.printStackTrace(); 168 }finally{ 169 closeAll(conn, pstmt, rs); 170 } 171 return row; 172 } 173 174 /** 175 * 关闭数据库连接 176 */ 177 public static void closeAll(Connection conn,Statement pstmt,ResultSet rs){ 178 try{ 179 if(rs!=null){ 180 rs.close(); 181 } 182 if(pstmt!=null){ 183 pstmt.close(); 184 } 185 if(conn!=null){ 186 conn.close(); 187 } 188 }catch (Exception e) { 189 e.printStackTrace(); 190 } 191 } 192 193 194 public static void main(String[] args) { 195 //System.out.println(getConnection()); 196 //save(new Student("刘二狗",888,new Date(System.currentTimeMillis()))); 197 /*List<Student> list = queryList(Student.class); 198 for(Student stu:list){ 199 System.out.println(stu.getId()+" "+stu.getName()+" "+stu.getMoney()+" "+stu.getBrithday()); 200 }*/ 201 delete(new Student(16)); 202 203 } 204 }
测试
1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 //1、获取hibernate中的配置信息(获取数据库的详细信息) 6 Configuration cofig = new Configuration().configure(); 7 //2、创建session工厂 8 SessionFactory factory = config.buildSessionFactory(); 9 //3、获取session对象(connection) 10 Session session = factory.openSession(); 11 //4、开启事务 12 session.beginTransaction(); 13 14 Student s = new Student(); 15 s.setAge(11); 16 s.setName("xsl"); 17 18 //5、保存 19 session.save(s); 20 //6、事务提交 21 session.getTransaction().commit(); 22 //7、session关闭 23 session.close(); 24 } 25 }