我正在尝试通过Java程序在MySql中创建表。在创建表的数据库查询中似乎有一个错误,我无法弄清楚。这是我的java程序
package Registration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StartBatch
{
public void startBatch(String batch)
{
Connection connection=null;
String connectionURL = "jdbc:mysql://localhost:3306/Batches";
Statement stmt;
ResultSet rs1;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
stmt=connection.createStatement();
rs1=stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `"+batch+"` (\n" +" `id` int(11) NOT NULL AUTO_INCREMENT,\n" +" `student` text NOT NULL,\n" +" `fees` bigint(20) NOT NULL,\n" +" PRIMARY KEY (`id`)\n" +") ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
//The error is in above query
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
}
}
}
}
错误:
java.lang.RuntimeException: Uncompilable source code - incompatible types: int cannot be converted to java.sql.ResultSet
at Registration.StartBatch.startBatch(StartBatch.java:39)
at org.apache.jsp.CreatedBatch_jsp._jspService(CreatedBatch_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
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:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我正在使用Netbeans来运行这个类。
答案 0 :(得分:0)
试试这个:
int updt;
updt=stmt.executeUpdate("CREATE TABLE IF NOT EXISTS " + batch + " (id int(11) NOT NULL AUTO_INCREMENT, student text NOT NULL, fees bigint(20) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)
答案 1 :(得分:0)
首先executeUpdate()
用于insert,delete and update
次查询,因此您不应将其用于创建表格。
如果您想使用Statement or
PreparedStatement
(大多数推荐)execute()
创建一个表,其返回类型为无效。
此外,您的stmt.executeUpdate(query)
返回int
类型而不是ResultSet
类型,请完成Documentation