下面将封装一个根据给定的表名,查询出表中的字段,然后生成相应的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);
//先查询出字段的备注并放入Map集合中
ps = con.prepareStatement("select COLUMN_NAME,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '"+tableName +"'");
rs = ps.executeQuery();
Map<String, String> map = new HashMap<String, String>();
while(rs.next()){
map.put(rs.getString(1), rs.getString(2));
}
/