问题描述
java添加(修改)一段查询的代码,查询的按钮是我自己添加的,但是功能不会写了。。。数据库(表)有学号、姓名、性别、年龄、班级、java成绩最后一个模块查询的窗口是直接复制的添加窗口。我的意思是用添加窗口,在里面输入要查询的内容后,点击按钮,在其余文本框显示出来其他内容。。。。代码太多了。。接二楼。。。。packagestudentinfo;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.table.*;importjava.sql.*;importcom.microsoft.sqlserver.jdbc.*;publicclassstudentinfo{//主类publicstaticvoidmain(Stringargs[]){newCDataFrame();//构造主窗口实例}}classCDataFrameextendsJFrameimplementsActionListener{JTabletable;//浏览表格JButtonbView,bEdit,bDelete,bInsert,bSou;Objecta[][];Connectioncon;Statementsql;ResultSetrs;StringsqlState;StringconUrl;ObjectcolumnName[]={"学号","姓名","性别","年龄","班级","Java成绩"};CDataFrame(){super("学生信息管理");a=newObject[40][6];TableModeldataModel=newAbstractTableModel(){//使用AbstractTableModel是为了使单元格不可编辑publicintgetColumnCount(){return6;}//设置列数publicintgetRowCount(){return40;}//设置行数publicStringgetColumnName(intcolumn){//设置列名return(String)columnName[column];}publicObjectgetValueAt(introw,intcol){returna[row][col];}//设置单元格值};/*此处还可使用以下语句table=newJTable(a,columnName);table.setModel(dataModel);*/table=newJTable(dataModel);setBounds(200,100,600,510);bView=newJButton("浏览全表");bEdit=newJButton("编辑记录");bDelete=newJButton("删除记录");bInsert=newJButton("添加记录");bSou=newJButton("查询记录");bView.addActionListener(this);//给按钮设置监听器bEdit.addActionListener(this);bDelete.addActionListener(this);bInsert.addActionListener(this);bSou.addActionListener(this);Containercontainer=getContentPane();container.setLayout(newFlowLayout());container.add(bView);container.add(bEdit);container.add(bDelete);container.add(bInsert);container.add(bSou);container.add(newJScrollPane(table),BorderLayout.CENTER);//向面板添加滚动窗口setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});try{//加载驱动程序Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//建立桥接器conUrl="jdbc:sqlserver://localhost:1433;databaseName=studentinfo;";//conUrl="jdbc:sqlserver://localhost:1434;databaseName=software0541;user=sa;password=sa;";//con=DriverManager.getConnection(conUrl);//sql=con.createStatement();//创建SQL语句sqlState="Select*fromstudent";//查询所有记录connect(sqlState,0);}catch(Exceptione){e.printStackTrace();}}inti=0;//处理标志publicvoidactionPerformed(ActionEventev){if(ev.getSource()==bView){clean();sqlState="Select*fromstudent";//查询所有记录connect(sqlState,0);}elseif(ev.getSource()==bEdit){intselNum=table.getSelectedRow();if(selNum==-1){JOptionPane.showMessageDialog(this,"请选择要编辑的记录!","错误提示",JOptionPane.INFORMATION_MESSAGE);return;}if(selNum>i-1){JOptionPane.showMessageDialog(this,"请选择已经存在的记录!","错误提示",JOptionPane.INFORMATION_MESSAGE);return;}StringstuNum=(String)a[selNum][0];CEditeditDialog=newCEdit(this,a[selNum]);sqlState=editDialog.getSql();if(!sqlState.equals(""))connect(sqlState,1);clean();sqlState="Select*fromstudent";connect(sqlState,0);}elseif(ev.getSource()==bDelete){intselNum=table.getSelectedRow();if(selNum==-1){JOptionPane.showMessageDialog(this,"请选择要删除的记录!","错误提示",JOptionPane.INFORMATION_MESSAGE);return;}if(selNum>i-1){JOptionPane.showMessageDialog(this,"请选择已经存在的记录!","错误提示",JOptionPane.INFORMATION_MESSAGE);return;}StringstuNum=(String)a[selNum][0];Object[]delOptions={"确定","取消"};intsel=JOptionPane.showOptionDialog(this,"您确认要删除该记录吗?","警告提示",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null,delOptions,delOptions[1]);if(sel==JOptionPane.YES_OPTION){sqlState="DeletefromstudentWhere学号='"+stuNum+"'";//学号为stuNumber的记录connect(sqlState,1);clean();sqlState="Select*fromstudent";connect(sqlState,0);}}elseif(ev.getSource()==bInsert){sqlState=insert();if(!sqlState.equals(""))connect(sqlState,1);clean();sqlState="Select*fromstudent";connect(sqlState,0);}elseif(ev.getSource()==bSou){sqlState=insert();if(!sqlState.equals(""))connect(sqlState,1);clean();sqlState="Select*fromstudentwhere学号=''";connect(sqlState,0);}}publicvoidclean(){while(i>=0){a[i][0]="";a[i][1]="";a[i][2]="";a[i][3]="";a[i][4]="";a[i][5]="";i--;}}publicvoidconnect(StringsqlState,intsel){try{con=DriverManager.getConnection(conUrl,"sa","123");sql=con.createStatement();//创建SQL语句if(sel==1){intres=sql.executeUpdate(sqlState);con.close();return;}elsers=sql.executeQuery(sqlState);//执行查询i=0;while(rs.next()){//遍历记录Stringnumber=rs.getString("学号");Stringname=rs.getString("姓名");Stringgender=rs.getString("性别");intage=rs.getInt("年龄");Stringclassname=rs.getString("班级");intscore=rs.getInt("Java成绩");a[i][0]=number;a[i][1]=name;a[i][2]=gender;a[i][3]=String.valueOf(age);a[i][4]=classname;a[i][5]=String.valueOf(score);i++;}con.close();//关闭连接repaint();//重绘组件}catch(SQLExceptione){e.printStackTrace();}}publicStringinsert(){CInsertinsertDialog=newCInsert(this);StringsqlState=insertDialog.getSql();returnsqlState;}}classCInsertextendsJDialogimplementsActionListener{Stringnumber="",name="",gender="",classname="";intage=0,score=0;JLabellabel1,label2,label3,label4,label5,label6;JTextFieldtf1,tf2,tf3,tf4,tf5,tf6;JButtonbutton1,button2;BooleanInsertYN=false;CInsert(Frameparent){super(parent,true);setTitle("添加新学生记录");this.setSize(300,400);setLocationRelativeTo(parent);this.addWindowListener(newWindowAdapter(){publicvoidwindowActivated(WindowEvente){//tf1.requestFocus();//设置初始化焦点位置}publicvoidwindowOpened(WindowEvente){tf1.requestFocus();//设置初始化焦点位置}});label1=newJLabel("学号:");label2=newJLabel("姓名:");label3=newJLabel("性别:");label4=newJLabel("年龄:");label5=newJLabel("班级:");label6=newJLabel("Java成绩:");tf1=newJTextField();tf2=newJTextField();tf3=newJTextField();tf4=newJTextField();tf5=newJTextField();tf6=newJTextField();button1=newJButton("提交");button2=newJButton("重置");button1.addActionListener(this);button2.addActionListener(this);Containerct=getContentPane();ct.setLayout(null);//使用自定义布局label1.setBounds(20,20,80,40);label2.setBounds(20,70,80,40);label3.setBounds(20,120,80,40);label4.setBounds(20,170,80,40);label5.setBounds(20,220,80,40);label6.setBounds(20,270,80,40);tf1.setBounds(120,20,160,40);tf2.setBounds(120,70,160,40);tf3.setBounds(120,120,160,40);tf4.setBounds(120,170,160,40);tf5.setBounds(120,220,160,40);tf6.setBounds(120,270,160,40);button1.setBounds(60,320,80,30);button2.setBounds(160,320,80,30);ct.add(label1);//加入标签ct.add(tf1);//加入文本框ct.add(label2);ct.add(tf2);ct.add(label3);ct.add(tf3);ct.add(label4);ct.add(tf4);ct.add(label5);ct.add(tf5);ct.add(label6);ct.add(tf6);ct.add(button1);//加入按钮ct.add(button2);setVisible(true);}publicvoidactionPerformed(ActionEventent){if(ent.getSource()==button1){number=tf1.getText().trim();name=tf2.getText().trim();gender=tf3.getText().trim();Stringsage=tf4.getText().trim();if(!sage.equals(""))age=Integer.parseInt(sage);classname=tf5.getText().trim();Stringsscore=tf6.getText().trim();if(!sscore.equals(""))score=Integer.parseInt(sscore);if(number.equals("")){JOptionPane.showMessageDialog(this,"学号不能为空","错误提示",JOptionPane.WARNING_MESSAGE);tf1.requestFocus();return;}elseif(name.equals("")){JOptionPane.showMessageDialog(this,"姓名不能为空","错误提示",JOptionPane.ERROR_MESSAGE);tf2.requestFocus();return;}elseif(gender.equals("")){JOptionPane.showMessageDialog(this,"性别不能为空","错误提示",JOptionPane.INFORMATION_MESSAGE);tf3.requestFocus();return;}elseif(sage.equals("")){JOptionPane.showMessageDialog(this,"年龄不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf4.requestFocus();return;}elseif(classname.equals("")){JOptionPane.showMessageDialog(this,"班级不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf5.requestFocus();return;}elseif(sscore.equals("")){JOptionPane.showMessageDialog(this,"Java成绩不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf6.requestFocus();return;}elsesetVisible(false);InsertYN=true;}elseif(ent.getSource()==button2){tf1.setText("");tf2.setText("");tf3.setText("");tf4.setText("");tf5.setText("");tf6.setText("");tf1.requestFocus();repaint();}}publicStringgetSql(){StringsqlState="Insertintostudentvalues('"+number+"','"+name+"','"+gender+"',"+age+",'"+classname+"',"+score+")";if(!InsertYN)sqlState="";returnsqlState;}}
解决方案
解决方案二:
classCEditextendsJDialogimplementsActionListener{Stringnumber="",name="",gender="",classname="";intage=0,score=0;JLabellabel1,label2,label3,label4,label5,label6;JTextFieldtf1,tf2,tf3,tf4,tf5,tf6;JButtonbutton1,button2;Objectdata[]=newObject[6];BooleanEditTF=false;CEdit(Frameparent,Objecta[]){super(parent,true);data=a;setTitle("添加学生记录");this.setSize(300,400);setLocationRelativeTo(parent);this.addWindowListener(newWindowAdapter(){publicvoidwindowActivated(WindowEvente){//tf1.requestFocus();//设置初始化焦点位置}publicvoidwindowOpened(WindowEvente){tf1.requestFocus();//设置初始化焦点位置}});label1=newJLabel("学号:");label2=newJLabel("姓名:");label3=newJLabel("性别:");label4=newJLabel("年龄:");label5=newJLabel("班级:");label6=newJLabel("Java成绩:");tf1=newJTextField();tf2=newJTextField();tf3=newJTextField();tf4=newJTextField();tf5=newJTextField();tf6=newJTextField();button1=newJButton("提交");button2=newJButton("重置");button1.addActionListener(this);button2.addActionListener(this);Containerct=getContentPane();ct.setLayout(null);//使用自定义布局label1.setBounds(20,20,80,40);label2.setBounds(20,70,80,40);label3.setBounds(20,120,80,40);label4.setBounds(20,170,80,40);label5.setBounds(20,220,80,40);label6.setBounds(20,270,80,40);tf1.setBounds(120,20,160,40);tf2.setBounds(120,70,160,40);tf3.setBounds(120,120,160,40);tf4.setBounds(120,170,160,40);tf5.setBounds(120,220,160,40);tf6.setBounds(120,270,160,40);button1.setBounds(60,320,80,30);button2.setBounds(160,320,80,30);tf1.setText((String)data[0]);tf2.setText((String)data[1]);tf3.setText((String)data[2]);tf4.setText((String)data[3]);tf5.setText((String)data[4]);tf6.setText((String)data[5]);ct.add(label1);//加入标签ct.add(tf1);//加入文本框ct.add(label2);ct.add(tf2);ct.add(label3);ct.add(tf3);ct.add(label4);ct.add(tf4);ct.add(label5);ct.add(tf5);ct.add(label6);ct.add(tf6);ct.add(button1);//加入按钮ct.add(button2);setVisible(true);}publicvoidactionPerformed(ActionEventent){if(ent.getSource()==button1){number=tf1.getText().trim();name=tf2.getText().trim();gender=tf3.getText().trim();Stringsage=tf4.getText().trim();if(!sage.equals(""))age=Integer.parseInt(sage);classname=tf5.getText().trim();Stringsscore=tf6.getText().trim();if(!sscore.equals(""))score=Integer.parseInt(sscore);if(number.equals("")){JOptionPane.showMessageDialog(this,"学号不能为空","错误提示",JOptionPane.WARNING_MESSAGE);tf1.requestFocus();return;}elseif(name.equals("")){JOptionPane.showMessageDialog(this,"姓名不能为空","错误提示",JOptionPane.ERROR_MESSAGE);tf2.requestFocus();return;}elseif(gender.equals("")){JOptionPane.showMessageDialog(this,"性别不能为空","错误提示",JOptionPane.INFORMATION_MESSAGE);tf3.requestFocus();return;}elseif(sage.equals("")){JOptionPane.showMessageDialog(this,"年龄不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf4.requestFocus();return;}elseif(classname.equals("")){JOptionPane.showMessageDialog(this,"班级不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf5.requestFocus();return;}elseif(sscore.equals("")){JOptionPane.showMessageDialog(this,"Java成绩不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf6.requestFocus();return;}elsesetVisible(false);EditTF=true;}elseif(ent.getSource()==button2){tf1.setText("");tf2.setText("");tf3.setText("");tf4.setText("");tf5.setText("");tf6.setText("");tf1.requestFocus();repaint();}}publicStringgetSql(){StringsqlState="Updatestudentset姓名='"+name+"',性别='"+gender+"',年龄="+age+",班级='"+classname+"',Java成绩="+score+"Where学号='"+number+"'";//StringsqlState="Updatestudentset姓名='abc',性别='男',年龄=22,班级='0541',Java成绩=72Where学号='01'";if(!EditTF)sqlState="";returnsqlState;}}//查询窗口classCSouextendsJDialogimplementsActionListener{Stringnumber="",name="",gender="",classname="";intage=0,score=0;JLabellabel1,label2,label3,label4,label5,label6;JTextFieldtf1,tf2,tf3,tf4,tf5,tf6;JButtonbutton1,button2;BooleanInsertYN=false;CSou(Frameparent){super(parent,true);setTitle("查询学生记录");this.setSize(300,400);setLocationRelativeTo(parent);this.addWindowListener(newWindowAdapter(){publicvoidwindowActivated(WindowEvente){//tf1.requestFocus();//设置初始化焦点位置}publicvoidwindowOpened(WindowEvente){tf1.requestFocus();//设置初始化焦点位置}});label1=newJLabel("学号:");label2=newJLabel("姓名:");label3=newJLabel("性别:");label4=newJLabel("年龄:");label5=newJLabel("班级:");label6=newJLabel("Java成绩:");tf1=newJTextField();tf2=newJTextField();tf3=newJTextField();tf4=newJTextField();tf5=newJTextField();tf6=newJTextField();button1=newJButton("提交");button2=newJButton("重置");button1.addActionListener(this);button2.addActionListener(this);Containerct=getContentPane();ct.setLayout(null);//使用自定义布局label1.setBounds(20,20,80,40);label2.setBounds(20,70,80,40);label3.setBounds(20,120,80,40);label4.setBounds(20,170,80,40);label5.setBounds(20,220,80,40);label6.setBounds(20,270,80,40);tf1.setBounds(120,20,160,40);tf2.setBounds(120,70,160,40);tf3.setBounds(120,120,160,40);tf4.setBounds(120,170,160,40);tf5.setBounds(120,220,160,40);tf6.setBounds(120,270,160,40);button1.setBounds(60,320,80,30);button2.setBounds(160,320,80,30);ct.add(label1);//加入标签ct.add(tf1);//加入文本框ct.add(label2);ct.add(tf2);ct.add(label3);ct.add(tf3);ct.add(label4);ct.add(tf4);ct.add(label5);ct.add(tf5);ct.add(label6);ct.add(tf6);ct.add(button1);//加入按钮ct.add(button2);setVisible(true);}publicvoidactionPerformed(ActionEventent){if(ent.getSource()==button1){number=tf1.getText().trim();name=tf2.getText().trim();gender=tf3.getText().trim();Stringsage=tf4.getText().trim();if(!sage.equals(""))age=Integer.parseInt(sage);classname=tf5.getText().trim();Stringsscore=tf6.getText().trim();if(!sscore.equals(""))score=Integer.parseInt(sscore);if(number.equals("")){JOptionPane.showMessageDialog(this,"学号不能为空","错误提示",JOptionPane.WARNING_MESSAGE);tf1.requestFocus();return;}elseif(name.equals("")){JOptionPane.showMessageDialog(this,"姓名不能为空","错误提示",JOptionPane.ERROR_MESSAGE);tf2.requestFocus();return;}elseif(gender.equals("")){JOptionPane.showMessageDialog(this,"性别不能为空","错误提示",JOptionPane.INFORMATION_MESSAGE);tf3.requestFocus();return;}elseif(sage.equals("")){JOptionPane.showMessageDialog(this,"年龄不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf4.requestFocus();return;}elseif(classname.equals("")){JOptionPane.showMessageDialog(this,"班级不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf5.requestFocus();return;}elseif(sscore.equals("")){JOptionPane.showMessageDialog(this,"Java成绩不能为空","错误提示",JOptionPane.QUESTION_MESSAGE);tf6.requestFocus();return;}elsesetVisible(false);InsertYN=true;}elseif(ent.getSource()==button2){tf1.setText("");tf2.setText("");tf3.setText("");tf4.setText("");tf5.setText("");tf6.setText("");tf1.requestFocus();repaint();}}publicStringgetSql(){StringsqlState="Insertintostudentvalues('"+number+"','"+name+"','"+gender+"',"+age+",'"+classname+"',"+score+")";if(!InsertYN)sqlState="";returnsqlState;}}