100分求解.
一个Bean:
/*
* 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);
}
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;
}
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);
}
}
运行成功
反回值是true时也没有把数据加入数据库.
问题点数:100、回复次数:8Top
1 楼simonxuluo(爱江山更爱美人)回复于 2005-04-03 23:47:34 得分 0
String condition="INSERT INTO student (id,name,password,mail,phone,contact,sex,postcode) VALUES('id','realName','password1','mail','phone','contact','sex','postcode')";
你这句有问题Top
2 楼simonxuluo(爱江山更爱美人)回复于 2005-04-03 23:48:25 得分 0
若数据库连接对的话,都把'id','realName','password1','mail','phone','contact','sex','postcode'插入数据库了Top
3 楼simonxuluo(爱江山更爱美人)回复于 2005-04-03 23:51:44 得分 0
按你的写法,应该改成
String condition="INSERT INTO student (id,name,password,mail,phone,contact,sex,postcode) VALUES('"+n1+"','"+n2+"','"+n3+"','"+n4+"','"+n5+"','"+n6+"','"+n7+"','"+n8+"')";Top
4 楼cpl3113(小龙)回复于 2005-04-04 08:42:38 得分 0
觉得你的数据库连接有问题,建议你这样检查
将异常处理catch改为{ e.printStackTrace();}
int rows = sql.executeUpdate(condition);
System.out.println("rows is :"+ rows);
看看有没有成功插入数据库
不知道你用的什么数据库,如果用orcle或sqlserver都要厂家提供的jar包做驱动
Top
5 楼xiejing1981314(新手)回复于 2005-04-04 09:01:55 得分 0
我加了int rows= sql.excuteUpdate(condition);
System.out.println("rows is :"+rows);
输出的结果是rows is 1
true;
这应该来说数据库的连接不有问题呀,可是数据就是没有加入数据库中.晕了~~!Top
6 楼lkpei(飞翔鸟)回复于 2005-04-04 09:08:54 得分 0
楼主是不是没有进行commit操作?另外你的数据不是“”就是null,放一个真实的数据试试。Top
7 楼daimojingdeyu(戴墨镜的鱼)回复于 2005-04-04 09:14:36 得分 100
sql.executeUpdate(condition);
在该句后加上
sql.close();
con.close();
这样试试Top
8 楼xiejing1981314(新手)回复于 2005-04-04 09:33:57 得分 0
我在main函数中有数据呀.Top




