用JDBC程序创建数据库表格
编写一个程序,可以通过此程序完成一个表的创建操作,输入表名称、各个列的各名称及类型,输入完成后直接通过JDBC创建指定的表。这个应该怎么写啊,是要定义一个sql语句,然后用Statement接口实现嘛,自己写了个,失败了,望请教……
public static void main(String[] args) { // TODO Auto-generated method stub String s_dirver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String s_url="jdbc:sqlserver://localhost:1433;"; String s_username="sa"; String s_pwd="shellingford"; String databaseName="test"; String tableName="testtable"; String sql="create database "+databaseName+";"; //列名、类型、约束 Object ob[][]={{"id","int","primary key"},{"name","varchar(20)",""},{"pwd","varchar(20)","unique"}}; StringBuilder sb=new StringBuilder(); sb.append("create table "+tableName+" ("); for(int i=0;i<ob.length;i++){ sb.append(ob[i][0]+" "+ob[i][1]+" "+ob[i][2]+" ,"); } if(sb.toString().endsWith(",")){ sb.deleteCharAt(sb.toString().length()-1); } sb.append(")"); Connection con =null; try { Class.forName(s_dirver); con = DriverManager.getConnection(s_url,s_username,s_pwd); con.setAutoCommit(false); PreparedStatement pstmt = con.prepareStatement(sql); pstmt.executeUpdate(); pstmt.close(); pstmt=con.prepareStatement("use "+databaseName+";"); pstmt.executeUpdate(); pstmt.close(); pstmt=con.prepareStatement(sb.toString()); pstmt.executeUpdate(); (); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); if(con!=null){ try { con.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }finally{ if(con!=null){ try { con.setAutoCommit(true); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public static void main(String[] args) { // TODO Auto-generated method stub String s_dirver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String s_url="jdbc:sqlserver://localhost:1433;"; String s_username="sa"; String s_pwd="shellingford"; String databaseName="test"; String tableName="testtable"; String sql="create database "+databaseName+";"; //列名、类型、约束 Object ob[][]={{"id","int","primary key"},{"name","varchar(20)",""},{"pwd","varchar(20)","unique"}}; StringBuilder sb=new StringBuilder(); sb.append("create table "+tableName+" ("); for(int i=0;i<ob.length;i++){ sb.append(ob[i][0]+" "+ob[i][1]+" "+ob[i][2]+" ,"); } if(sb.toString().endsWith(",")){ sb.deleteCharAt(sb.toString().length()-1); } sb.append(")"); Connection con =null; try { Class.forName(s_dirver); con = DriverManager.getConnection(s_url,s_username,s_pwd); con.setAutoCommit(false); Statement st=con.createStatement(); st.execute(sql); st.execute("use "+databaseName+";"); st.execute(sb.toString()); st.close(); (); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); if(con!=null){ try { con.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }finally{ if(con!=null){ try { con.setAutoCommit(true); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }