当前位置: 技术问答>java相关
高手教我-关于数据库数据的删除(在线等待)
来源: 互联网 发布时间:2015-11-12
本文导语: 忙了一上午,怎么也没有搞明白。 我的源程序如下: 删除不用的数据 您输入的记录已被删除 另外,数据库采用:access 数据源名:panchang 表: employee 其中表中字段: ...
忙了一上午,怎么也没有搞明白。
我的源程序如下:
删除不用的数据
另外,数据库采用:access
数据源名:panchang
表: employee
其中表中字段:
id 数字
name 文本
gender 文本
age 数字
出错信息如下:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 苃?-pn{?9M
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6106)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6263)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:2525)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
at _jsp._website._del__jsp._jspService(/website/del.jsp:20)
at com.caucho.jsp.JavaPage.service(JavaPage.java:89)
at com.caucho.jsp.JavaPage.subservice(JavaPage.java:83)
at com.caucho.jsp.Page.service(Page.java:280)
at com.caucho.jsp.QServlet.service(QServlet.java:161)
at com.caucho.server.http.AbstractRequest.service(AbstractRequest.java:452)
at com.caucho.server.http.AbstractRequest.service(AbstractRequest.java:395)
at com.caucho.server.http.PageCache$Entry.service(PageCache.java:256)
at com.caucho.server.http.PageCache.service(PageCache.java:105)
at com.caucho.server.http.VirtualHost.service(VirtualHost.java:406)
at com.caucho.server.http.Request.dispatch(Request.java:211)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:307)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:257)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:536)
我的源程序如下:
删除不用的数据
您输入的记录已被删除
另外,数据库采用:access
数据源名:panchang
表: employee
其中表中字段:
id 数字
name 文本
gender 文本
age 数字
出错信息如下:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 苃?-pn{?9M
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6106)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6263)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:2525)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
at _jsp._website._del__jsp._jspService(/website/del.jsp:20)
at com.caucho.jsp.JavaPage.service(JavaPage.java:89)
at com.caucho.jsp.JavaPage.subservice(JavaPage.java:83)
at com.caucho.jsp.Page.service(Page.java:280)
at com.caucho.jsp.QServlet.service(QServlet.java:161)
at com.caucho.server.http.AbstractRequest.service(AbstractRequest.java:452)
at com.caucho.server.http.AbstractRequest.service(AbstractRequest.java:395)
at com.caucho.server.http.PageCache$Entry.service(PageCache.java:256)
at com.caucho.server.http.PageCache.service(PageCache.java:105)
at com.caucho.server.http.VirtualHost.service(VirtualHost.java:406)
at com.caucho.server.http.Request.dispatch(Request.java:211)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:307)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:257)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:536)
|
两处错误:
1.delete中不使用*
2.字段类型不一致,id是数字,不要加引号,但如果mystr是String类型的,则需要转换
mystr1="delete from employee where id = " + Integer.parseInt(mystr);
1.delete中不使用*
2.字段类型不一致,id是数字,不要加引号,但如果mystr是String类型的,则需要转换
mystr1="delete from employee where id = " + Integer.parseInt(mystr);
|
你试一下将异常打印出来,看一下是什么问题
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:panchang");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
mystr1="delete * from employee where id = '" + mystr + "'";
stmt.executeUpdate(mystr1);
stmt.close();
conn.close();
}catch (SQLException e){out.print(e);}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:panchang");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
mystr1="delete * from employee where id = '" + mystr + "'";
stmt.executeUpdate(mystr1);
stmt.close();
conn.close();
}catch (SQLException e){out.print(e);}
|
将mystr1="delete * from employee where id = '" + mystr + "'";
改成mystr1="delete * from employee where id = " + mystr + ";
因为你的字段id是数字,不是文本类型
改成mystr1="delete * from employee where id = " + mystr + ";
因为你的字段id是数字,不是文本类型
|
你创建的state是只读的