当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 我在客户端用exp导出了服务器上的数据库,想用这个导出文件在本地机子上恢复一个数据库。我是这样做的:在本地新建了一个数据库,然后用imp导入,却没有成功,我想是不是 要建跟服务器上数据库一模一样的表空间呢? ......
    摘要: 我的数据窗口由三个表的列组成,其中有一个表是我默认的可更新表,它有一个主键,但当我新增一行时,我竟然不能在这个字段的编辑框里写入数据!这是为什么?? ......


bean中填写方法问题,高手帮忙啊

我是java初学者,想在javabean中写几个方法,例如删,增,改,查询,但在写在bean的过程中,总是遇到很多麻烦,请大家指教!!!谢谢了,程序如下,现在的增加还有很多毛病,请大家帮助我一下!!!!

NO.1   作者: qiyao

说来你是写的是实体Bean,  
  这样吧,我给你一个例子,对你有帮助:  
  //CustomerBean.java  
   
   
  import   java.sql.*;  
  import   javax.sql.*;  
  import   java.util.*;  
  import   javax.ejb.*;  
  import   javax.naming.*;  
   
  public   class   CustomerBean   implements   EntityBean   {  
   
        private   String   customerId;  
        private   String   salesRepId;  
        private   String   name;  
        private   Connection   con;  
        private   String   dbName   =   "java:comp/env/jdbc/SalesDB";  
        private   EntityContext   context;  
   
   
        public   String   getSalesRepId()   {  
   
              return   salesRepId;  
        }  
           
        public   String   getName()   {  
   
              System.out.println("entering   getName()");  
              return   name;  
        }  
   
        public   void   setSalesRepId(String   salesRepId)   {  
   
              this.salesRepId   =   salesRepId;  
        }  
           
        public   void   setName(String   name)   {  
   
              this.name   =   name;  
        }  
   
        public   String   ejbCreate(String   customerId,   String   salesRepId,  
                String   name)   throws   CreateException   {  
         
                System.out.println("in   ejbCreate");  
                try   {  
                      insertCustomer(customerId,   salesRepId,   name);  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbCreate:   "   +    
                              ex.getMessage());  
                }  
   
                this.customerId   =   customerId;  
                this.salesRepId   =   salesRepId;  
                this.name   =   name;  
   
                System.out.println("about   to   leave   ejbCreate");  
                return   customerId;  
        }  
             
        public   String   ejbFindByPrimaryKey(String   primaryKey)    
              throws   FinderException   {  
   
              boolean   result;  
   
              try   {  
                    result   =   selectByPrimaryKey(primaryKey);  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbFindByPrimaryKey:   "   +    
                              ex.getMessage());  
                }  
   
              if   (result)   {  
                    return   primaryKey;  
              }  
              else   {  
                    throw   new   ObjectNotFoundException  
                          ("Row   for   id   "   +   primaryKey   +   "   not   found.");  
              }  
        }  
   
        public   Collection   ejbFindBySalesRep(String   salesRepId)  
              throws   FinderException   {  
   
              Collection   result;  
   
              try   {  
                    result   =   selectBySalesRep(salesRepId);  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbFindBySalesRep:   "   +    
                              ex.getMessage());  
                }  
                return   result;  
        }  
   
        public   void   ejbRemove()   {  
   
              try   {  
                    deleteCustomer(customerId);  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbRemove:   "   +    
                              ex.getMessage());  
                }  
        }    
   
        public   void   setEntityContext(EntityContext   context)   {  
   
              this.context   =   context;  
              try   {  
                    makeConnection();  
              }   catch   (Exception   ex)   {  
                      throw   new   EJBException("Unable   to   connect   to   database.   "   +  
                            ex.getMessage());  
              }  
        }  
   
        public   void   unsetEntityContext()   {  
   
              try   {  
                    con.close();  
              }   catch   (SQLException   ex)   {  
                      throw   new   EJBException("unsetEntityContext:   "   +   ex.getMessage());  
              }  
        }  
   
        public   void   ejbActivate()   {  
   
              customerId   =   (String)context.getPrimaryKey();  
        }  
   
        public   void   ejbPassivate()   {  
   
              customerId   =   null;  
        }  
         
        public   void   ejbLoad()   {  
   
              System.out.println("in   ejbLoad");  
              try   {  
                    loadCustomer();  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbLoad:   "   +  
                              ex.getMessage());  
                }  
              System.out.println("leaving   ejbLoad");  
        }  
   
        public   void   ejbStore()   {  
   
              System.out.println("in   ejbStore");  
              try   {  
                    storeCustomer();  
                }   catch   (Exception   ex)   {  
                        throw   new   EJBException("ejbStore:   "   +  
                              ex.getMessage());  
                }  
              System.out.println("leaving   ejbStore");  
        }  
   
        public   void   ejbPostCreate(String   customerId,   String   salesRepId,  
                  String   name)   {   }  
   
  /***********************   Database   Routines   *************************/  
   
        private   void   makeConnection()   throws   NamingException,   SQLException   {  
   
              InitialContext   ic   =   new   InitialContext();  
              DataSource   ds   =   (DataSource)   ic.lookup(dbName);  
              con   =     ds.getConnection();  
        }  
   
        private   void   insertCustomer   (String   customerId,   String   salesRepId,  
                String   name)   throws   SQLException   {  
   
                      String   insertStatement   =  
                                  "insert   into   customer   values   (   ?   ,   ?   ,   ?   )";  
                      PreparedStatement   prepStmt   =    
                                  con.prepareStatement(insertStatement);  
   
                      prepStmt.setString(1,   customerId);  
                      prepStmt.setString(2,   salesRepId);  
                      prepStmt.setString(3,   name);  
   
                      prepStmt.executeUpdate();  
                      prepStmt.close();  
        }  
   
        private   boolean   selectByPrimaryKey(String   primaryKey)    
              throws   SQLException   {  
   
              String   selectStatement   =  
                          "select   customerid   "   +  
                          "from   customer   where   customerid   =   ?   ";  
              PreparedStatement   prepStmt   =  
                          con.prepareStatement(selectStatement);  
              prepStmt.setString(1,   primaryKey);  
   
              ResultSet   rs   =   prepStmt.executeQuery();  
              boolean   result   =   rs.next();  
              prepStmt.close();  
              return   result;  
        }  
   
        private   Collection   selectBySalesRep(String   salesRepId)    
              throws   SQLException   {  
   
              String   selectStatement   =  
                          "select   customerid   "   +  
                          "from   customer   where   salesrepid   =   ?   ";  
              PreparedStatement   prepStmt   =    
                          con.prepareStatement(selectStatement);  
   
              prepStmt.setString(1,   salesRepId);  
              ResultSet   rs   =   prepStmt.executeQuery();  
              ArrayList   a   =   new   ArrayList();  
   
              while   (rs.next())   {  
                    String   id   =   rs.getString(1);  
                    a.add(id);  
              }  
   
              prepStmt.close();  
              return   a;  
        }  
   
        private   void   deleteCustomer(String   customerId)   throws   SQLException   {  
   
              String   deleteStatement   =  
                          "delete   from   customer     "   +  
                          "where   customerid   =   ?";  
              PreparedStatement   prepStmt   =  
                          con.prepareStatement(deleteStatement);  
   
              prepStmt.setString(1,   customerId);  
              prepStmt.executeUpdate();  
              prepStmt.close();  
        }  
   
        private   void   loadCustomer()   throws   SQLException   {  
   
              String   selectStatement   =  
                          "select   customerid,   salesRepid,   name   "   +  
                          "from   customer   where   customerid   =   ?   ";  
              PreparedStatement   prepStmt   =    
                          con.prepareStatement(selectStatement);  
   
              prepStmt.setString(1,   customerId);  
   
              ResultSet   rs   =   prepStmt.executeQuery();  
   
              if   (rs.next())   {  
                    customerId   =   rs.getString(1);  
                    salesRepId   =   rs.getString(2);  
                    name   =   rs.getString(3);  
                    prepStmt.close();  
              }  
              else   {  
                    prepStmt.close();  
                    throw   new   NoSuchEntityException("Row   for   customerId   "   +   customerId   +  
                          "   not   found   in   database.");  
              }  
        }  
   
        private   void   storeCustomer()   throws   SQLException   {  
   
              System.out.println("entering   storeCustomer");  
              String   updateStatement   =  
                          "update   customer   "   +  
                          "set   salesRepid   =   ?   ,   name   =   ?   "   +  
                          "where   customerid   =   ?";  
              PreparedStatement   prepStmt   =    
                          con.prepareStatement(updateStatement);  
   
              prepStmt.setString(1,   salesRepId);  
              prepStmt.setString(2,   name);  
              prepStmt.setString(3,   customerId);  
              int   rowCount   =   prepStmt.executeUpdate();  
              prepStmt.close();  
   
              if   (rowCount   ==   0)   {  
                    throw   new   EJBException("Storing   row   for   customerId   "   +    
                          customerId   +   "   failed.");  
              }  
              System.out.println("leaving   storeCustomer");  
        }  
   
  }   //   CustomerBean    
 


    摘要: [config] key1 = 000 请问如何删除key1项? ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE