自動生成テーブルのフィールド名
3788 ワード
package jdbc.test;
import java.sql.*;
/**
* @author jone
*
*/
public class JDBCDemo {
static String user ="ewingad" ;
static String password ="ewingadzhidian3g.com.cn";
static String url = "";
static String tableName="";
static String driver ="com.mysql.jdbc.Driver";
static Connection con = null;
static {
try {
Class.forName(driver);
initInfo(1);
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void initInfo(int type) {
switch (type) {
case 1:
url = "jdbc:mysql://192.168.0.14:3306/cloudBoxCms?useUnicode=true&characterEncoding=utf-8";
tableName = "sysUser";
break;
default:
url = "jdbc:mysql://localhost:3306/box_log_copy";
tableName="box_mobile_log";
break;
}
}
/**
* @param args
*/
public static void main(String[] args) {
String sqlstr = "select * from "+tableName;
querySql(tableName, sqlstr);
}
private static void querySql(String tableName, String sqlstr) {
Statement stmt = null;
ResultSet rs = null;
try{
stmt = con.createStatement();
// sqlstr = "insert into "+tableName+" values ('20140113','jone',01)";
// stmt.executeUpdate(sqlstr);
rs = stmt.executeQuery(sqlstr);
StringBuilder sb=new StringBuilder();
ResultSetMetaData rsmd = rs.getMetaData();
int j = 0;
j = rsmd.getColumnCount();
for(int k = 0; k<j; k++)
{
// System.out.print(rsmd.getCatalogName(k+1));
System.out.print("'"+rsmd.getColumnName(k+1)+"'"+",");
//System.out.print("\t");
sb.append(rsmd.getColumnName(k+1)).append(",");
}
System.out.println();
System.err.println(sb.deleteCharAt(sb.length()-1).toString());
// 。
// while(rs.next())
// {
// for(int i=0;i<j;i++)
// {
// System.out.print(rs.getString(i+1));
// System.out.print("\t");
// }
// System.out.println();
// }
}catch(SQLException e2)
{
System.out.println(" !");
System.out.println(e2.toString());
}
finally
{
try
{
colseConection(stmt, rs);
}
catch(SQLException e)
{
System.out.println(e.toString());
}
}
}
private static void colseConection(Statement stmt, ResultSet rs)
throws SQLException {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
}
}
自動生成テーブルのフィールド名