周帆.
今日学习总结:
今天学了javabean的定义,数据库结果集的操作,学习了八个专门用于操作结果集的类 有阿帕奇提供的DBUtils包中提供(虽然斌哥说以后基本上用不到 我还是老老实实敲了一天),主要的练习是在java端对数据库进行操作 下面贴上部分代码示例:
操作结果集的类的部分代码示例
1:public static void mapListHandler()throws Exception{
String sql = "SELECT *FROM sort";
List<Map<String,Object>> lis = qr.query(con, sql, new MapListHandler());
for(Map<String,Object> map : lis){
for(String key : map.keySet()){
System.out.print(key+"..."+map.get(key));
}
System.out.println();
}
}
2:public static void scalarHandler()throws Exception{
String sql = "SELECT COUNT(*)FROM sort";
Long l = qr.query(con, sql, new ScalarHandler<Long>());
System.out.println(l);
}
自己定义的静态工具类 用于连接数据库
public class JDBCUtils {
private static Connection con;
private static String driverClass;
private static String url;
private static String username;
private static String password;
private static Properties prop;
static{
try {
InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("database.properties");
prop = new Properties();
prop.load(in);
readerConfig();
} catch (Exception e) {
throw new RuntimeException(e+"数据库连接失败或者配置文件读取失败");
}
}
public static Connection getConnection(){
return con;
}
private static void readerConfig()throws Exception{
driverClass = prop.getProperty("driverClass");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
con = DriverManager.getConnection(url, username, password);
}
}
总结结束
good good study day day up!!!!!!
|
|