我这样写SQL有错吗?
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{System.out.print(e);
}
try {con=DriverManager.getConnection("jdbc:odbc:jxpt","","");
sql=con.createStatement();
String condition="INSERT INTO student (id,name,password,mail,phone,contact,sex,postcode) VALUES(id,realName,password1,mail,phone,contact,sex,postcode)";
sql.executeUpdate(condition);
}
catch(SQLException e)
{
this.re=false;
System.out.println(e);
}
return this.re;
}
我在运行时出现java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver]参数不足,期待是8
这是什么原因呀?
问题点数:20、回复次数:9Top
1 楼jFresH_MaN(十一月的萧邦-夜曲)回复于 2005-04-03 21:46:02 得分 20
String condition="INSERT INTO student (id,name,password,mail,phone,contact,sex,postcode) VALUES(id,realName,password1,mail,phone,contact,sex,postcode)";
----------
values里面如果是字符或者字符串型的必须用单引号‘
比如
insert into tableName (name) values('ccc');Top
2 楼SunnyTomorrow(SunnyTomorrow)回复于 2005-04-03 22:57:08 得分 0
唉。。。不知道怎么说你Top
3 楼xiejing1981314(新手)回复于 2005-04-03 22:58:11 得分 0
如果是布尔值不用加吧.Top
4 楼xiejing1981314(新手)回复于 2005-04-03 22:59:25 得分 0
想怎么说都行,菜鸟一只.:(Top
5 楼xiejing1981314(新手)回复于 2005-04-03 23:14:20 得分 0
对字符串型加了'编译成功了.
可是对数据库插不进数据.郁闷!~~~Top
6 楼joyaga(joyaga)回复于 2005-04-04 09:23:57 得分 0
你把源文件都贴出来帮你看看
Top
7 楼xiejing1981314(新手)回复于 2005-04-04 09:30:48 得分 0
好的.全部的代码.
/*
* Created on 2005-3-29
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package bean;
import java.sql.*;
import java.util.ArrayList;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
/**
* @author xiejing
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class UserinfoBean {
private String id="";
private String realName="";
private String password1="";
private String phone="";
private String mail="";
private String sex="";
private String address="";
private String postcode="";
private Connection con=null;
private Statement sql=null;
private boolean re=true;
private ResultSet rs=null;
public boolean register(String n1,String n2,String n3,String n4,String n5,String n6,String n7, String n8) {
this.id=n1;
this.realName=n2;
this.password1=n3;
this.sex=n4;
this.phone=n5;
this.address=n6;
this.mail=n7;
this.postcode=n8;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{ //System.out.print(e);
e.printStackTrace();
}
try { con=DriverManager.getConnection("jdbc:odbc:jxpt","","");
sql=con.createStatement();
String condition="INSERT INTO student (id,name,password,mail,phone,contact,sex,postcode) VALUES('"+n1+"','"+n2+"','"+n3+"','"+n7+"','"+n5+"','"+n6+"','"+n4+"','"+n8+"')";
int rows=sql.executeUpdate(condition);
System.out.println("rows is:" +rows);
}
catch(SQLException e)
{
this.re=false;
//System.out.println(e);
e.printStackTrace();
}
return this.re;
}
public static void main(String[] args) {
UserinfoBean ub=new UserinfoBean();
boolean b=ub.register( "sy0406630","xiaozhan","6202637","M","82337280","buaa","xiaozhan@163.com","100082");
System.out.print(b);
}
}
最后的输出结果是:rows is:1
true
可是数据没有加入到数据库中.Top
8 楼joyaga(joyaga)回复于 2005-04-04 09:41:25 得分 0
都 执行成功了 你看抛出什么错误没Top
9 楼wtiancai(博学,审问,慎思,明辨,笃行.)回复于 2005-04-04 09:51:08 得分 0
out.print(condition)
然后把这条语句拿到数据库里面运行,看能不能执行成功,不能执行它就会提示哪里出错了Top




