CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

100分求解.

楼主xiejing1981314(新手)2005-04-03 23:27:49 在 Java / Web 开发 提问

一个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

相关问题

  • 求解 求解 求解
  • 求解??
  • 求解?
  • 求解A
  • 求解?
  • 求解
  • 求解。。。
  • 求解
  • 求解
  • 求解

关键词

  • code
  • sql
  • null
  • private
  • generation
  • comments
  • import

得分解答快速导航

  • 帖主:xiejing1981314
  • daimojingdeyu

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo