问题描述
这是Dao类packagecom.lzw.dao;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.lzw.model.TbCusinfo;importcom.lzw.model.Tfoperater;publicclassDao{protectedstaticStringdbClassName="net.sourceforge.jtds.jdbc.Driver";protectedstaticStringdbUrl="jdbc:jtds:sqlserver://localhost:1433;"+"DatabaseName=db_test;SelectMethod=Cursor";protectedstaticStringdbUser="sa";protectedstaticStringdbPass="";publicstaticConnectionconn=null;static{try{if(conn==null){Class.forName(dbClassName).newInstance();conn=DriverManager.getConnection(dbUrl,dbUser,dbPass);}}catch(Exceptione){e.printStackTrace();}}privateDao(){}//新增方法publicstaticbooleaninsert(Stringsql){booleanresult=false;try{Statementst=conn.createStatement();result=st.execute(sql);}catch(SQLExceptione){e.printStackTrace();}returnresult;}//修改方法publicstaticintupdate(Stringsql){intresult=0;try{if(conn==null)newDao();Statementstmt=conn.createStatement();result=stmt.executeUpdate(sql);}catch(Exceptione){e.printStackTrace();}returnresult;}//删除方法publicstaticintdelete(Stringsql){returnupdate(sql);}//查询方法publicstaticResultSetquery(Stringsql){ResultSetresult=null;try{if(conn==null)newDao();Statementstmt=conn.createStatement();result=stmt.executeQuery(sql);}catch(Exceptione){e.printStackTrace();}returnresult;}//关闭方法publicstaticvoidclose(){try{conn.close();}catch(Exceptione){e.printStackTrace();}}//读取用户方法publicstaticTfoperaterselectUser(Stringnameno,Stringpassword){Tfoperatertfoperater=newTfoperater();Stringsql="select*fromtb_operatorwherenameno='"+nameno+"'andpassword='"+password+"'";ResultSetrs=query(sql);try{if(rs.next()){tfoperater.setNameno(nameno);tfoperater.setPassword(password);}}catch(Exceptione){e.printStackTrace();}finally{try{rs.close();}catch(SQLExceptione){e.printStackTrace();}}//Dao.close();returntfoperater;}//执行指定查询publicstaticResultSetzdquery(Stringzdno){ResultSetset=query(zdno);returnset;}//添加客户信息的方法publicstaticbooleanaddCus(TbCusinfotbcusinfo){if(tbcusinfo==null)returnfalse;returninsert("inserttb_cusinfovalues('"+tbcusinfo.getCus_no()+"','"+tbcusinfo.getName()+"','"+tbcusinfo.getSnm()+"','"+tbcusinfo.getPost()+"','"+tbcusinfo.getTel()+"','"+tbcusinfo.getFax()+"','"+tbcusinfo.getTelpople()+"','"+tbcusinfo.getAddress()+"','"+tbcusinfo.getBank()+"','"+tbcusinfo.getBankno()+"','"+tbcusinfo.getEmail()+"')");}publicstaticListfindforlist(Stringsql){List<List>list=newArrayList<List>();ResultSetrs=query(sql);try{ResultSetMetaDatametaData=rs.getMetaData();intcolCount=metaData.getColumnCount();while(rs.next()){List<String>row=newArrayList<String>();for(inti=1;i<=colCount;i++){Stringstr=rs.getString(i);if(str!=null&&!str.isEmpty())str=str.trim();row.add(str);}list.add(row);}}catch(Exceptione){e.printStackTrace();}returnlist;}//读取所有客户信息名称publicstaticListgetCust(){Listlist=findforlist("selectnamefromtb_cusinfo");returnlist;}}这个是窗体显示类packageinternal.cus;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.ArrayList;importjava.util.Collection;importjava.util.Iterator;importjava.util.List;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;importcom.lzw.dao.Dao;importcom.lzw.item.Item;importcom.lzw.model.TbCusinfo;publicclassCustModiextendsJFrame{privateJComboBoxcus_no;privateJComboBoxname;privateJTextFieldsnm;privateJTextFieldpost;privateJTextFieldtel;privateJTextFieldfax;privateJTextFieldtelpople;privateJTextFieldemail;privateJTextFieldaddress;privateJTextFieldbank;privateJTextFieldbankno;privateJButtonbuttonadd;privateJButtonbuttonesit;publicCustModi(){super();setTitle("客户信息修改与删除");setLayout(null);setBounds(190,160,650,300);finalJLabellabel_1=newJLabel("客户编号:");label_1.setBounds(15,10,100,18);getContentPane().add(label_1);cus_no=newJComboBox();//initComboBox();//初始化客户下拉框cus_no.setBounds(100,10,100,18);getContentPane().add(cus_no);finalJLabellabel_2=newJLabel("客户全称:");label_2.setBounds(220,10,100,18);getContentPane().add(label_2);name=newJComboBox();initComboBox();//初始化客户下拉框name.setBounds(300,10,300,18);getContentPane().add(name);finalJLabellabel_3=newJLabel("客户简称:");label_3.setBounds(15,40,100,18);getContentPane().add(label_3);snm=newJTextField();snm.setBounds(100,40,200,18);getContentPane().add(snm);finalJLabellabel_4=newJLabel("邮政编码:");label_4.setBounds(315,40,100,18);getContentPane().add(label_4);post=newJTextField();post.setBounds(395,40,205,18);getContentPane().add(post);finalJLabellabel_5=newJLabel("电话:");label_5.setBounds(15,70,100,18);getContentPane().add(label_5);tel=newJTextField();tel.setBounds(100,70,200,18);getContentPane().add(tel);finalJLabellabel_6=newJLabel("传真:");label_6.setBounds(315,70,100,18);getContentPane().add(label_6);fax=newJTextField();fax.setBounds(395,70,205,18);getContentPane().add(fax);finalJLabellabel_7=newJLabel("联系人:");label_7.setBounds(15,100,100,18);getContentPane().add(label_7);telpople=newJTextField();telpople.setBounds(100,100,200,18);getContentPane().add(telpople);finalJLabellabel_8=newJLabel("电邮:");label_8.setBounds(315,100,100,18);getContentPane().add(label_8);email=newJTextField();email.setBounds(395,100,205,18);getContentPane().add(email);finalJLabellabel_9=newJLabel("地址:");label_9.setBounds(15,130,100,18);getContentPane().add(label_9);address=newJTextField();address.setBounds(100,130,500,18);getContentPane().add(address);finalJLabellabel_10=newJLabel("开户银行:");label_10.setBounds(15,160,100,18);getContentPane().add(label_10);bank=newJTextField();bank.setBounds(100,160,200,18);getContentPane().add(bank);finalJLabellabel_11=newJLabel("银行账号:");label_11.setBounds(315,160,100,18);getContentPane().add(label_11);bankno=newJTextField();bankno.setBounds(395,160,205,18);getContentPane().add(bankno);buttonadd=newJButton("删除");//buttonadd.addActionListener(newbuttonaddActionListener());buttonadd.setBounds(200,220,100,22);getContentPane().add(buttonadd);buttonesit=newJButton("修改");buttonesit.setBounds(300,220,100,22);getContentPane().add(buttonesit);}//初始化客户名称下拉框publicvoidinitComboBox(){Listtbcusinfo=Dao.getCust();List<TbCusinfo>items=newArrayList<TbCusinfo>();name.removeAllItems();for(Iteratoriter=tbcusinfo.iterator();iter.hasNext();){Listelement=(List)iter.next();TbCusinfotbc=newTbCusinfo();//tbc.setCus_no(element.get(0).toString().trim());tbc.setName(element.get(0).toString().trim());if(items.contains(tbc))items.add(tbc);name.addItem(tbc);}}//启动窗体信息publicstaticvoidlaunch(){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newCustModi().setVisible(true);}});}}运行之后,在窗体名称下拉框下面会显示错误信息:显示:com.lzw.model.TbCusinfo@bf7190这是我引入的一个包名。
解决方案
解决方案二:
这个类的toString()方法没有重写
解决方案三:
publicvoidinitComboBox(){Listtbcusinfo=Dao.getCust();List<TbCusinfo>items=newArrayList<TbCusinfo>();name.removeAllItems();for(Iteratoriter=tbcusinfo.iterator();iter.hasNext();){Listelement=(List)iter.next();TbCusinfotbc=newTbCusinfo();//tbc.setCus_no(element.get(0).toString().trim());tbc.setName(element.get(0).toString().trim());if(items.contains(tbc))items.add(tbc);name.addItem(tbc);}红色部分的那个类里的toString()方法没有重写
解决方案四:
该回复于2011-01-07 17:03:10被版主删除