我们首先看下baseJDBC的写法实例:
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class baseJDBC {
// 表示你要操作的是哪种类型的数据库
private final String DRIVER = "com.mysql.jdbc.Driver";
// 表示你要连接的是哪一台电脑的服务器端口号是多少数据库的名字是什么
private final String URL = "jdbc:mysql://localhost:3306/zzy";//有时这里需要加上字符集
// 登录数据库的用户名
private final String USERNMAE = "root";
// 登录数据库的密码
private final String PASSWORD = "root";
public Connection getConnection() {
try {
//Driver d=new Driver();
// 注册驱动:反射(是一项很高深的技术)
Class.forName(DRIVER);
// 由连接大管家创建连接对象
return DriverManager.getConnection(URL, USERNMAE, PASSWORD);
} catch (ClassNotFoundException e) {
//e.printStackTrace("数据库的驱动文件没有找到");
} catch (SQLException e) {
//数据库的连接错误
e.printStackTrace();
}
return null;
}
public void closeAll(Connection con, Statement st, ResultSet rt) {
try {
if (rt != null) {
rt.close();
rt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
CRUDDAO 写法代码实例:
package com.dao; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.sql.*; import java.util.*; import java.util.Map.Entry; public class CRUDDAOextends baseJDBC { private Connection con = null; private PreparedStatement pt = null; private Statement st = null; private ResultSet rt = null; private Class c; public CRUDDAO() { } public CRUDDAO(Class c) { this.c = c; } public Map > selectAll(Map m) { int index = 0; Map > map = new linkedHashMap >(); List list = null; try { con = super.getConnection(); if (con != null) { Set > set = m.entrySet(); for (Entry entry : set) { list = new ArrayList (); pt = con.prepareStatement(entry.getKey()); this.bind(entry.getValue()); rt = pt.executeQuery(); while (rt.next()) { list.add(this.toBean2()); } map.put(++index, list); } } else { System.out.println("数据库连接失败"); } } catch (SQLException e) { e.printStackTrace(); } finally { super.closeAll(con, pt, rt); } return map; } private T toBean() { T t = null; try { t = c.newInstance(); Method[] m = c.getMethods(); ResultSetmetaData rmt = rt.getmetaData(); for (int i = 1, count = rmt.getColumnCount(); i <= count; i++) { String columName = rmt.getColumnName(i); columName = "set" + columName.substring(0, 1).toUpperCase() + columName.substring(1); for (int j = 0; j < m.length; j++) { if (columName.equals(m[j].getName())) { m[j].invoke(t, rt.getObject(i)); break; } } } } catch (Exception e) { e.printStackTrace(); } return t; } private T toBean2() { T t = null; try { // 创建反射类的实例 t = c.newInstance(); // 反射出所有字段 Field[] field = c.getDeclaredFields(); for (Field f : field) { // 根据反射的字段名得到数据库中的字段值 Object value = rt.getObject(f.getName()); f.setAccessible(true);// 打开私有字段的操作权限 f.set(t, value);// 调用这个字段的公有的set方法封装字段的值 } } catch (Exception e) { e.printStackTrace(); } return t; } private void bind(Object[] obj) { try { if (obj != null) { for (int i = 0, k = obj.length; i < k; i++) { pt.setObject(i + 1, obj[i]); } } } catch (SQLException e) { e.printStackTrace(); } } public int[] updateAll(Map map) { int[] row = new int[map.size()]; int index = 0; int error = 0; try { con = super.getConnection(); if (con != null) { Set > set = map.entrySet(); // 关闭连接对象的自动提交的功能 con.setAutoCommit(false); for (Entry entry : set) { pt = con.prepareStatement(entry.getKey()); this.bind(entry.getValue()); row[index] = pt.executeUpdate(); if (row[index] == 0) { throw new Exception("修改失败,数据回滚!"); } index++; } } else { System.out.println("数据库连接失败"); } } catch (Exception e) { error++; e.printStackTrace(); } finally { if (error > 0) { try { // 将前面已经执行的命令回滚 con.rollback(); } catch (SQLException e) { e.printStackTrace(); } } else { try { // 全部提交 con.commit(); } catch (SQLException e) { e.printStackTrace(); } } super.closeAll(con, st, null); } return row; } }
总结
以上就是本文关于baseJDBC和CRUDDAO的写法实例代码的全部内容,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



