下面将封装一个根据给定的表名,查询出表中的字段,然后生成相应的Po类。
public static void creationPoClass(String tableName,String path){
String url = "jdbc:mysql://localhost:3306/lending";//数据源(mysql)的连接地址
String user = "root";//用户名
String password = "root";//用户密码
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
//拼接SQL语句,用于查询出该表的所有字段
String sql = "select * from " + tableName;
try {
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//获取连接
con = DriverManager.getConnection(url, user, password);
//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
ps = con.prepareStatement(sql);
//执行查询语句,执行后返回代表查询结果的ResultSet对象。
rs = ps.executeQuery();
//获取此 ResultSet 对象的列的编号、类型和属性。
ResultSetMetaData rsmd = rs.getMetaData();
//获取表中的列数
int count = rsmd.getColumnCount();
//把表名的首字母大写,用作于Po类的类名
tableName = tableName.substring(0, 1).toUpperCase() + tableName.substring(1);
//拼接Po类的路径
path = path+"//"+tableName+"Po.java";
File file = new File(path);
//创建一个新的空文件。
file.createNewFile();
//创建输出流
OutputStream out = new FileOutputStream(file);
//拼接Po类中的数据
StringBuffer sbf = new StringBuffer();
sbf.append("package com.wlx.po;\n\n");
sbf.append("import java.io.Serializable;\n\n");
sbf.append("public class "+tableName+"Po implements Serializable{\n\n" );
//生成字段属性
for(int i=1;i<=count;i++){
//把字段类型转换成小写
String type = rsmd.getColumnTypeName(i).toLowerCase();
//把数据库的数据类型改成java数据类型(alterType()该方法在下面单独封装了起来)
type = alterType(type);
//把字段名称首字母转成小写(如果不需要转,则直接使用rsmd.getColumnLabel(i)即可)
String name = rsmd.getColumnLabel(i).substring(0, 1).toLowerCase() + rsmd.getColumnLabel(i).substring(1);
sbf.append("\tprivate "+type+" "+name+";\n\n");
}
//生成get和set方法
for(int i=1;i<=count;i++){
//把字段类型转换成小写
String type = rsmd.getColumnTypeName(i).toLowerCase();
//把数据库的数据类型改成java数据类型(alterType()该方法在下面单独封装了起来)
type = alterType(type);
//把字段名称首字母小写
String name = rsmd.getColumnLabel(i).substring(0, 1).toLowerCase() + rsmd.getColumnLabel(i).substring(1);
String getsetName = rsmd.getColumnLabel(i);
//get方法
sbf.append("\tpublic "+type+" get"+getsetName+"() {\n\t\treturn "+ name+";\n\t}\n");
//set方法
sbf.append("\tpublic void set"+getsetName+"("+type+" "+ name +") {\n\t\tthis."+ name+" = "+name+";\n\t}\n");
}
sbf.append("}");
out.write(sbf.toString().getBytes());
out.flush();
out.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
//关闭资源
if(rs !=null){
rs.close();
}
if(ps !=null){
ps.close();
}
if(con !=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面转换类型的方法在下面这里单独封装了起来,这里仅仅转换了几个常用的类型,根据需要自行添加
public static String alterType(String type){
//当数据库中的数据类型与java类型不一致时,就转换成相应的java类型
if(type.equalsIgnoreCase("varchar") || type.equalsIgnoreCase("char") || type.equalsIgnoreCase("datetime") || type.equalsIgnoreCase("date")){
type = "String";
}
if(type.equalsIgnoreCase("binary")){
type = "byte";
}
if(type.equalsIgnoreCase("tinyint")){
type = "byte";
}
return type;
}
调用该方法
public static void main(String[] asgr){
creationPoClass("borrowing","src//com//wlx//po");//参数一是数据库中的表名,参数二是生成Po类的目标路径
}