问题描述
javaswing做的登陆界面,怎样判断数据库中用户名和密码?之前代码用户名和密码是定死的,现在想改成从数据库的表中读出Name和Password字段与文本框里输入的比较。。在网上差了很多,说要查询数据库,遍历第一个字段。。请问代码应该怎么改啊,本人基础差。。还请各位帮帮忙原来的代码是:packagecom.finance.ui;importjava.awt.*;importjavax.swing.*;importjava.awt.Rectangle;importjavax.swing.BorderFactory;importcom.finance.dao.*;importcom.finance.ui.LoginFrame;importcom.finance.ui.LoginFrame.LoginFrame_btnOK_actionAdapter;importcom.finance.ui.MainUiFrame;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.*;publicclassLoginFrameextendsJFrame{publicstaticvoidmain(String[]args){LoginFramemainFrame=newLoginFrame();mainFrame.setVisible(true);}publicLoginFrame(){try{this.setDefaultCloseOperation(EXIT_ON_CLOSE);jbInit();DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();this.setResizable(false);this.setTitle("家庭理财-用户登录");this.setSize(400,220);DimensionframeSize=this.getSize();if(frameSize.height>screenSize.height){frameSize.height=screenSize.height;}if(frameSize.width>screenSize.width){frameSize.width=screenSize.width;}this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);this.setVisible(true);}catch(Exceptionexception){exception.printStackTrace();}}privatevoidjbInit()throwsException{getContentPane().setLayout(null);pnlContainer.setBounds(newRectangle(0,0,400,220));pnlContainer.setLayout(null);lblUserName.setText("用户名:");lblUserName.setBounds(newRectangle(105,81,55,18));txtUserName.setBounds(newRectangle(173,81,120,18));lblPWD.setText("密 码:");lblPWD.setBounds(newRectangle(105,119,55,18));txtPWD.setBounds(newRectangle(173,119,120,18));jLabel1.setFont(newjava.awt.Font("宋体",Font.BOLD,18));jLabel1.setBorder(BorderFactory.createEtchedBorder());jLabel1.setHorizontalAlignment(SwingConstants.CENTER);jLabel1.setText("欢迎使用家庭理财小秘书");jLabel1.setBounds(newRectangle(77,20,235,45));btnOK.setBounds(newRectangle(91,157,81,23));btnOK.setText("确定");btnOK.addActionListener(newLoginFrame_btnOK_actionAdapter(this));btnCancel.setBounds(newRectangle(197,157,81,23));btnCancel.setText("取消");this.getContentPane().add(pnlContainer);pnlContainer.add(jLabel1);pnlContainer.add(lblUserName);pnlContainer.add(lblPWD);pnlContainer.add(btnOK);pnlContainer.add(btnCancel);pnlContainer.add(txtUserName);pnlContainer.add(txtPWD);}publicbooleangetFlag(){returnthis.flag;}JPanelpnlContainer=newJPanel();JLabellblUserName=newJLabel();JTextFieldtxtUserName=newJTextField();JLabellblPWD=newJLabel();JPasswordFieldtxtPWD=newJPasswordField();JLabeljLabel1=newJLabel();JButtonbtnOK=newJButton();JButtonbtnCancel=newJButton();privatebooleanflag=false;publicvoidbtnOK_actionPerformed(ActionEvente){if("jyf".equals(this.txtUserName.getText())&&"123".equals(newString(this.txtPWD.getPassword()))){System.out.println(this.txtUserName.getText()+this.txtPWD.getPassword());this.setFlag(true);booleanpackFrame=false;MainUiFrameframe=newMainUiFrame();//Validateframesthathavepresetsizes//Packframesthathaveusefulpreferredsizeinfo,e.g.fromtheirlayoutif(packFrame){frame.pack();}else{frame.validate();}//CenterthewindowDimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();DimensionframeSize=frame.getSize();if(frameSize.height>screenSize.height){frameSize.height=screenSize.height;}if(frameSize.width>screenSize.width){frameSize.width=screenSize.width;}frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);System.out.println(this.getFlag());if(this.getFlag()){frame.setVisible(true);}else{}}else{System.out.println("密码错误!"+this.txtUserName.getText()+this.txtPWD.getPassword());this.flag=false;JOptionPane.showMessageDialog(this,"用户名或密码错误","登录失败",JOptionPane.ERROR_MESSAGE);this.txtUserName.requestFocus();this.txtUserName.selectAll();}}publicvoidsetFlag(booleanflag){this.flag=flag;}classLoginFrame_btnOK_actionAdapterimplementsActionListener{privateLoginFrameadaptee;LoginFrame_btnOK_actionAdapter(LoginFrameadaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.btnOK_actionPerformed(e);}}}数据库链接文件写好的packagecom.finance.dao;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.*;importjava.sql.*;publicclassDataBase{privatestaticConnectioncon=null;publicstaticConnectiongetCon(){try{if(con==null){Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");con=DriverManager.getConnection("jdbc:odbc:finance","sa","sa");}}catch(SQLExceptionex){ex.printStackTrace();}catch(ClassNotFoundExceptionex){ex.printStackTrace();}returncon;}}
解决方案
解决方案二:
你先要写一个查询是SQL语句查找用户名和密码,然后在去获得窗体中的用户名和密码,最后在进行判断。。。
解决方案三:
能不能说的具体点,在哪里添加代码?publicbooleanloginByNameAndPassword(Stringname,Stringpassword){Stringsql="select*fromtbl_userwhereName='"+name+"'andPassword='"+password+"'";try{Connectionconnection=this.getCon();PreparedStatementpstat=connection.prepareStatement(sql);ResultSetrs=pstat.executeQuery();if(rs.next()){returntrue;}}catch(Exceptione){System.out.println(e.getMessage());returnfalse;}returnfalse;}这个类添加到DataBase类里,然后if判断后要怎么改?具体代码是什么?
解决方案四:
LZ,还是去看看基础的java视频教程吧。
解决方案五:
MainFrame类在哪啊?
解决方案六:
你应该朝着这种思路去写:客户端:就是一个用户名,密码的输入框。然后用一个类去封装。然后用objecOutputStrem将这个对象发送到服务器端,得到那个类的用户名,密码,调用你的数据库语句,在进行判断。。。
解决方案七:
谢谢你的提示