当前位置: 代码迷 >> Java相关 >> 用JDBC程序创建数据库表格
  详细解决方案

用JDBC程序创建数据库表格

热度:459   发布时间:2010-08-09 22:01:49.0
用JDBC程序创建数据库表格
编写一个程序,可以通过此程序完成一个表的创建操作,输入表名称、各个列的各名称及类型,输入完成后直接通过JDBC创建指定的表。
这个应该怎么写啊,是要定义一个sql语句,然后用Statement接口实现嘛,自己写了个,失败了,望请教……
搜索更多相关的解决方案: 数据库  JDBC  表格  

----------------解决方案--------------------------------------------------------
DDL和DML不一样,DDL很多数据库都有些差别的,特别是自动增长列等等,具体要看你想弄什么数据库了。
按照输入表名、列名、类型把DDL拼出来就是了。
----------------解决方案--------------------------------------------------------
回复 楼主 didozhp
程序代码:

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


sql server 2005下正常,代码很简单。
如果想复杂需要修改ob为List,放入多个参数并重新写拼DDL语句的代码。
如果约束或者外键、索引等更复杂的建议在建表后用DDL语句修改表。
----------------解决方案--------------------------------------------------------
回复 2楼 shellingford
那Statement接口只能对DML进行操作嘛,我是看到Statement接口下有个boolean execute(String sql)throws SQLException方法,说是进行执行SQL语句操作的,所以试了下,结果不行……
----------------解决方案--------------------------------------------------------
回复 4楼 didozhp
程序代码:
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();
            con.commit();
        } 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();
                }
            }
        }
    }

也可以创建啊

如果你那里失败的话,最好贴上你的代码或者报错信息。
----------------解决方案--------------------------------------------------------
回复 5楼 shellingford
好的,谢谢
----------------解决方案--------------------------------------------------------
  相关解决方案