public void table(String name, String col1, String type1, String null1, String key) throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS ? (id INT NOT NULL AUTO_INCREMENT, ? ? ? , PRIMARY KEY (?))";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "`myDb`."+name);
stmt.setString(2, col1);
stmt.setString(3, type1);
stmt.setString(4, null1);
stmt.setString(5, key);
stmt.executeUpdate();
}
Servlet controller.java:
if (action.equals("createTable")) {
String name = request.getParameter("name");
String null3 = request.getParameter("null1");
String type3 = request.getParameter("type1");
String key = request.getParameter("key");
CreateTable createTable = new CreateTable(conn1);
try {
if (col2 == "" && col3 == "" && col4 == "" && col5 == "" && col6 == "" && col7 == "" && col8 == "" && col9 == "" && col10 == "" && col11 == "" && col12 == "") {
createTable.table(name, col1, type1, null1, key);
request.getRequestDispatcher("/memberpanel.jsp").forward(request, response);
// request.setAttribute("message", "");
response.setContentType("text/html");
out.println("<script type=\"text/javascript\">");
out.println("window.alert('Your table created successfully')");
out.write("setTimeout(function(){window.location.href='memberpanel.jsp'},1000);");
out.write("setTimeout(function(){window.location.href='Index.jsp'},1000);");
out.println("</script>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.getMessage();
}
执行时,我收到以下信息:
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 ''`myDb`.admin' (id INT NOT NULL AUTO_INCREMENT, 'rescue' 'VARCHAR(750)' 'NOT NUL' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 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:1052) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2347) at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97) at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:97) at queries.CreateTable.table(CreateTable.java:478) at controller.Controller.doPost(Controller.java:504) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
myDb是我的数据库。admin、rescue、not null、varchar(750)、rescue是表名、COL的测试值。名称、空值或非空值、数据类型和主键。
不能对“非值”占位符使用这样的查询参数。
相反,在Java中将查询构建为单个字符串,然后执行该字符串。
您可以使用string.format()
获得类似的内容:
String query = String.format("CREATE TABLE IF NOT EXISTS %s (id INT NOT NULL AUTO_INCREMENT, %s %s %s , PRIMARY KEY (%s))", "`myDb`."+name, col1, type1, null1, key);
问题内容: 我正在尝试在MySQL中生成一个大的连续数字表。 我只想要2列;一个主键和一个数字列,范围在0-X之间,其中X很大。大约 应该有64,000行。我尝试了以下代码,但没有成功: 我得到这个错误: #1064-您的SQL语法有误;在第3行的“ CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED(number))INSERT INTO n”附近检查与您的M
我有一个< code>_payments数据库,其中有一个< code>users表,表中有< code>id 、< code >用户名 、< code >密码。密码使用散列盐。我的开发人员不可用,我需要创建一个新用户。为此,我应该运行什么查询?
WordPress有一个管理仪表板。在仪表板中,我们可以作为管理员添加新用户。我想在管理员添加新用户时在MySQL中创建一个表。例如,我创建了一个名为John Smith的用户,其用户名为user1;当我成功添加该用户时,将在名为user1的数据库中创建一个表。
问题内容: 我正在将MS Access应用程序(已将表链接到MSSQL Server)迁移到MySQL。 作为克服某些MSAccess表命名问题的方法,我正在寻找一种解决方案以添加一个MySQL表别名,该别名将指向MySQL数据库中的现有表。理想情况下,我想在mysql中创建别名“ dbo_customers”,该别名也指向mysql中的customers表。 需要明确的是,我 不 希望别名这样的
下面是我的dispatcher-servlet.xml文件: http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context/spring-context
问题内容: 我正在使用MySQL。我有一个名为EMP的表,现在我需要再创建一个具有相同架构,相同列和相同约束的表(EMP_TWO)。我怎样才能做到这一点? 问题答案: 要基于其他表结构/约束创建新表,请使用: 要跨多个数据复制,请使用 创建表格文档 当心关于LIKE选项的注意事项: 使用LIKE根据另一个表的定义(包括原始表中定义的任何列属性和索引)创建一个空表: CREATE TABLE new