您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > sqlserver数据库查询(图形界面)
publicclassSqlserverQueryGUI{/***@paramargs*/JLabelfirstJLabel=newJLabel(username);JLabelsecondJLabel=newJLabel(result);JTextFieldTextField=newJTextField(20);JTextAreaTextArea=newJTextArea(5,20);JScrollPanejsp=newJScrollPane(TextArea);JButtonlogin=newJButton(query);JPanelp1=newJPanel();JPanelp2=newJPanel();JPanelp3=newJPanel();publicSqlserverQueryGUI(){JFramef=newJFrame(查询);f.setSize(500,300);f.setLocation(200,200);f.setLayout(newGridLayout(4,1));//改变调整容器布局//f.setLayout(null);p1.add(firstJLabel);p1.add(TextField);f.add(p1);p2.add(secondJLabel);p2.add(jsp);f.add(p2);login.addActionListener(newExecuteCalc());//建立按钮和事件处理者的关系;注册监听p3.add(login);f.add(p3);f.setVisible(true);}//内部类:委托式事件处理classExecuteCalcimplementsActionListener{publicvoidactionPerformed(ActionEvente){Stringoperator=e.getActionCommand();Stringusername=TextField.getText();if(operator.equals(query)){try{Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);Connectionconn=DriverManager.getConnection(jdbc:sqlserver://localhost:1433;DatabaseName=master,sa,sa);if(conn!=null){System.out.println(sqlserver数据库连接成功);}Stringsql=select*fromloginwherev_un='+username+';Statementstmt=conn.createStatement();ResultSetrs=stmt.executeQuery(sql);while(rs.next()){Stringpasswords=rs.getString(v_psw);TextArea.append(username++passwords+\n);System.out.println(passwords);}}catch(ClassNotFoundExceptionexc){JOptionPane.showMessageDialog(null,程序错误,提示框,JOptionPane.ERROR_MESSAGE);exc.printStackTrace();}catch(SQLExceptionexc){JOptionPane.showMessageDialog(null,程序错误,提示框,JOptionPane.ERROR_MESSAGE);exc.printStackTrace();}}}}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubnewSqlserverQueryGUI();}}
本文标题:sqlserver数据库查询(图形界面)
链接地址:https://www.777doc.com/doc-2860283 .html