使用Mysql中的executeUpdate通过sql语句创建表 [英] Create table in by sql statement using executeUpdate in Mysql

查看:108
本文介绍了使用Mysql中的executeUpdate通过sql语句创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 doGet()

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MysqlDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setServerName("localhost");
        ds.setPort(3306);
        ds.setUser("root");
        ds.setPassword("");
        try {
            Connection connection = null ; 
            connection =  ds.getConnection();
            Statement statement = connection.createStatement();
            // create the DB .. 
            statement.executeUpdate("CREATE DATABASE IF NOT EXISTS "
                            +"testSQLtable" )  ;

            // use DB ..

            statement.executeUpdate("USE testSQLtable") ; 

            // create table ...
            statement.executeUpdate("DROP TABLE IF EXISTS  account "
                    +"CREATE TABLE  account ( "
                     +"accountNum INT( 11 ),"
                     +"dateCreated DATE NOT NULL,"
                     +"accountName TEXT,"
                     +"description TEXT,"
                     +"statusAccount TEXT,"
                     +"sumOfMoney INT( 11 ) NOT NULL DEFAULT  0 );" )  ;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

前两个 executeUpdate 创建数据库并使用他工作正常。在运行时到达行 statement.executeUpdate(DROP TABLE IF EXISTS accounnt .....它会引发异常 -

The first two executeUpdate of create DB and use him works fine . When in running reach to the line statement.executeUpdate("DROP TABLE IF EXISTS accounnt ....." it throws an exception -

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE  account ( accountNum INT( 11 ),dateCreated DATE NOT NULL,accountNa' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2713)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1794)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1712)
    at control.CreateDBinitialServlet.doGet(CreateDBinitialServlet.java:56)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


推荐答案

这应该适合你的情况:

statement.executeUpdate("DROP TABLE IF EXISTS  account ");
statement.executeUpdate("CREATE TABLE  account ( "
                     +"accountNum INT( 11 ),"
                     +"dateCreated DATE NOT NULL,"
                     +"accountName TEXT,"
                     +"description TEXT,"
                     +"statusAccount TEXT,"
                     +"sumOfMoney INT( 11 ) NOT NULL DEFAULT  0 )" )  ;

原因:语句只能在每次调用execute-methods时执行一个SQL语句。

Cause: Statements can only execute one SQL-Statement with every call of the execute-methods.

如果要同时执行两个或多个语句,可以使用批处理作业执行此操作。

赞:

If you want to execute two or more statements simultaneously you can do this with Batch-Jobs.
Like:

statement.addBatch("DROP TABLE IF EXISTS  account ");
statement.addBatch("CREATE TABLE  account ( "
                     +"accountNum INT( 11 ),"
                     +"dateCreated DATE NOT NULL,"
                     +"accountName TEXT,"
                     +"description TEXT,"
                     +"statusAccount TEXT,"
                     +"sumOfMoney INT( 11 ) NOT NULL DEFAULT  0 )" )  ;
statement.executeBatch();

这篇关于使用Mysql中的executeUpdate通过sql语句创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆