读书人

为什么插入数据时抛出sqlerror错误

发布时间: 2011-12-25 23:21:20 作者: rapoo

为什么插入数据时抛出sqlerror异常
我要向数据库中添加新的数据,可是每次执行的时候总会抛出SQLerror的错误,这是什么原因呢
我调试了下 在执行PreparedStatement pst=con.prepareStatement(str)这条语句时中断的,不过不知道应该怎么改。
<!--buy.jsp-->
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>

<html>
<head>
<title> 添加新书 </title>
</head>
<body bgcolor= "#C0C0C0 ">
<center>
<%
if(session.isNew())
{
%>
<center>
<h1> 添加新书信息 </h1> <br>
<form method= "post " action= "buy.jsp " name= "form ">
ID <input type= "text " size= "20 " maxlength= "20 " name= "name ">
<br>

书名 <input type= "text " size= "20 " maxlength= "20 " name= "name ">
<br>
作者 <input type= "password " size= "20 " maxlength= "20 " name= "author ">
<br>
价格 <input type= "text " size= "20 " maxlength= "20 " name= "price ">
<br>
数量 <input type= "text " size= "20 " maxlength= "20 " name= "amount ">
<br>
出版社 <input type= "text " size= "20 " maxlength= "20 " name= "product ">
<br>
类别 <input type= "text " size= "20 " maxlength= "20 " name= "type ">
<br>
<input type= "submit " value= "提交 " name= "submit ">
<input type= "reset " value= "重置 " name= "reset ">
</center>
<%
}
else
{
try{
String id=request.getParameter( "id ");
if(id==null)
id= " ";
String author=request.getParameter( "author ");
if(author==null)
author= " ";
String type=request.getParameter( "type ");
if(type==null)
type= " ";
String name=request.getParameter( "name ");
if(name==null)
name= " ";
String price=request.getParameter( "price ");
if(price==null)
price= " ";
String amount=request.getParameter( "amount ");
if(amount==null)
amount= " ";
String product=request.getParameter( "product ");
if(product==null)
product= " ";
//Class.forName( "DriverManager.JdbcOdbcDriver ");
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
//////////////////////////////////////////////////
String strDirPath=getServletContext().getRealPath( "/ ");
out.println(strDirPath);
//获得所在站点的绝对路径:E:\javatest\
//out.print(strDirPath+ " <br> ");
strDirPath=strDirPath.replace( '\\ ', '/ ');
out.print(strDirPath);
//将“\”替换为“/” E:/javatest/
//out.print(strDirPath+ " <br> ");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ= "+strDirPath+ "Library/library.mdb ";


////////////////////////////////////////////////////////////////////////////////
//con=DriverManager.getConnection( "jdbc:odbc:myshop.mdb ", " ", " ");
out.println( "开始数据库加载! ");
Connection con=DriverManager.getConnection(url, " ", " ");
out.println( "数据库加载成功! ");//
//Connection con=DriverManager.getConnection( "jdbc:odbc:myshop ", " ", " ");
String str= "insert into book values(?,?,?,?,?,?,?) ";
PreparedStatement pst=con.prepareStatement(str);
pst.setString(1,id);
pst.setString(2,name);
pst.setString(3,author);
pst.setString(4,price);
pst.setString(5,amount);
pst.setString(6,product);
pst.setString(7,type);
pst.executeUpdate();
pst.close();
con.close();
response.sendRedirect( "index.jsp ");
}
catch(SQLException e){
out.print( "sqlerror ");
}
catch(ClassNotFoundException e){
out.print( "CNFerror ");
}
}
%>

</center>
</body>
</html>


[解决办法]
很可能是你数据库中book表没有那么多列名,也就是说你插入的值和数据库表中的值不匹配

也有可能还是你插入的值数据类型和数据库中不同
[解决办法]
out.println( "数据库加载成功! ");如果这句话打印出来了,那就是楼上说的错误如果没打出来,那就是连接数据库的url写错了,你可以先写成固定的字符串测试一下
[解决办法]
报的什么错误?你数据库中有表吗?
插入语句没有错误,你先去库里试一下
[解决办法]
请给出后台错误的提示,这样这么多的代码眼睛都看花了

读书人网 >J2SE开发

热点推荐