自动生成表的字段名

 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();
	}
 }

 自动生成表的字段名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值