DataBaseMetaData对象:由Connection.getDataBaseMetaData()方法获得,可以用来获取数据库的元数据,提供的方法有:
- getURL():返回一个String类,代表数据库的URL,例:jdbc:mysql://localhost:3306/dataname;
- getUserName:返回连接数据库管理系统的用户名,例:root@localhost;
- getDatabaseProductName():返回数据库的产品名,例:MYSQL
- getDatabaseProductVersion():返回数据库的版本号,例:5.1.44-community;
- getDriverName():返回驱动程序名;例:MySQL-AB JDBC Driver;
- getDriverVersion():返回驱动程序的版本号;例:mysql-connector-java-5.0.8;
- isReadOnly():返回布尔值,表示是否只允许读;
ParametrMetaData对象:由PrepareStatement.getParameterMetaData()方法获得,可以用来获取PreparedStatement对象的元数据,常用的方法有:
- getParameterCount():获得指定参数的个数;
- getParameterType(int):获取指定参数的sql类型;
String sql = "select * from user where name = ? and password = ?"; PreparedStatement st = conn.PrepareStatement(sql); ParameterMetaData pm = st.getParameterMetaData(); pm.getParameterCount(); pm.getParameterType(1);
ResultSetMetaData对象:由ResultSet.getMetaData()方法获得,用来获取ResultSet对象的元数据,常用的方法:
- getColumnCount():返回resultset对象的列数;
- getColumnName(int column):获得指定的列名;
- getColumnTypeName(int column):获得指定列的类型;
开发一个jdbcUtils工具类:
package lzj.learn; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JdbcUtils{ private static String driver = null; private static String url = null; private staric String username = null; private static String password = null; //读取db.properties文件,对参数都赋予值; static{ try{ InputStream in = Jdbc.Utils.class.getClassLoader().getResourseAsStream("db.properties"); Porperties prop = new Properties(); prop.load(in); driver = prop.getProperty("driver"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); Class.forName(driver); }catch(Exception e) { throw new ExceptionInInitializeError(e); } } //连接数据库,获取连接对象 public static Connection getConnection() throws SQLException{ return DriverManafer.getConnection(url,username,password); } //释放资源 public static void release(Connection conn,Statement st,ResultSet rs) { if(rs!=null){ try{ rs.close(); }catch (Exception e) { e.printStackTrace(); } rs = null; } if(st!=null){ try{ st.close(); }catch (Exception e){ e.printStackTrace(); } } if(conn!=null){ try{ conn.close(); }catch{ e.printStackTrace(); } } } //更新数据库;sql参数:要执行的SQL语句;params[]:执行时使用的参数 public static void update (String sql , Object params[]) throws SQLException{ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = getConnection(); st = conn.prepareStatement(sql); for(int = 0 ;i<params.length;i++){ st.setObject(i+1,params[i]); } st.executeUpdate(); }finally{ release(conn,st,rs); } } //查询数据库,与更新数据库不同的是,查询数据库多提供了一个处理结果集的处理器 public static Object query(String sql ,Object params[],ResultSetHandler rsh) throws SQLException{ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = getConnection(); st = conn.prepareStatement(sql); for(int i = 0;i<params.length;i++){ st.setObject(i+1,params[i]); } rs = st.executeQuery(); return rsh.handler(rs);//将查询的结果集交给ResultSetHandler类,再通过定义ResultSetHandler类,实现handler方法即可实现对结果集的操作; }finally{ release(conn,st,rs); } } }
对处理器接口ResultSetHandler定义可如下:
package lzj.learn; import java.sql.ResultSet; //结果集处理接口 public interface ResultSetHandler{ public Object handler (ResultSet rs);//通过实现handler方法实现对结果集的操作 }
有了上述的框架后,就可以针对单个实体对象进行CRUD了;
packagec lzj.learn; import java.sql.SQLException; import java.util.List; import me.gacl.domain.Account; import me.gacl.util.BeanHandler; import me.gacl.util.BeanListHandler; import me.gacl.util.JdbcUtils; public class AccountDao { public void add(Account account) throws SQLException{ String sql = "insert into account(name,money) values(?,?)"; Object params[] = {account.getName(),account.getMoney()}; JdbcUtils.update(sql, params); } public void delete(int id) throws SQLException{ String sql = "delete from account where id=?"; Object params[] = {id}; JdbcUtils.update(sql, params); } public void update(Account account) throws SQLException{ String sql = "update account set name=?,money=? where id=?"; Object params[] = {account.getName(),account.getMoney(),account.getId()}; JdbcUtils.update(sql, params); } public Account find(int id) throws SQLException{ String sql = "select * from account where id=?"; Object params[] = {id}; return (Account) JdbcUtils.query(sql, params, new BeanHandler(Account.class)); } public List<Account> getAll() throws SQLException{ String sql = "select * from account"; Object params[] = {}; return (List<Account>) JdbcUtils.query(sql, params,new BeanListHandler(Account.class)); } }