java swing做的登陆界面,怎样判断数据库中用户名和密码?

jyfwyf 2010-05-23 05:24:48
java swing做的登陆界面,怎样判断数据库中用户名和密码?

之前代码用户名和密码是定死的,现在想改成从数据库的表中读出Name和Password字段与文本框里输入的比较。。在网上差了很多,说要查询数据库,遍历第一个字段。。请问代码应该怎么改啊,本人基础差。。还请各位帮帮忙
原来的代码是:
package com.finance.ui;

import java.awt.*;


import javax.swing.*;

import java.awt.Rectangle;
import javax.swing.BorderFactory;
import com.finance.dao.*;
import com.finance.ui.LoginFrame;

import com.finance.ui.LoginFrame.LoginFrame_btnOK_actionAdapter;
import com.finance.ui.MainUiFrame;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;


public class LoginFrame extends JFrame {
public static void main(String[] args) {
LoginFrame mainFrame = new LoginFrame();
mainFrame.setVisible(true);
}
public LoginFrame() {
try {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setResizable(false);
this.setTitle("家庭理财-用户登录");
this.setSize(400,220);
Dimension frameSize = 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 (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
pnlContainer.setBounds(new Rectangle(0, 0, 400, 220));
pnlContainer.setLayout(null);
lblUserName.setText("用户名:");
lblUserName.setBounds(new Rectangle(105, 81, 55, 18));
txtUserName.setBounds(new Rectangle(173, 81, 120, 18));
lblPWD.setText("密 码:");
lblPWD.setBounds(new Rectangle(105, 119, 55, 18));
txtPWD.setBounds(new Rectangle(173, 119, 120, 18));
jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 18));
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("欢迎使用家庭理财小秘书");
jLabel1.setBounds(new Rectangle(77, 20, 235, 45));
btnOK.setBounds(new Rectangle(91, 157, 81, 23));
btnOK.setText("确定");
btnOK.addActionListener(new LoginFrame_btnOK_actionAdapter(this));
btnCancel.setBounds(new Rectangle(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);
}
public boolean getFlag(){
return this.flag;
}
JPanel pnlContainer = new JPanel();
JLabel lblUserName = new JLabel();
JTextField txtUserName = new JTextField();
JLabel lblPWD = new JLabel();
JPasswordField txtPWD = new JPasswordField();
JLabel jLabel1 = new JLabel();
JButton btnOK = new JButton();
JButton btnCancel = new JButton();
private boolean flag=false;



public void btnOK_actionPerformed(ActionEvent e) {
if("jyf".equals(this.txtUserName.getText())&&"123".equals(new String(this.txtPWD.getPassword()))){
System.out.println(this.txtUserName.getText()+this.txtPWD.getPassword());
this.setFlag(true);


boolean packFrame = false;
MainUiFrame frame = new MainUiFrame();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}

// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = 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();
}

}

public void setFlag(boolean flag) {
this.flag = flag;
}


class LoginFrame_btnOK_actionAdapter implements ActionListener {
private LoginFrame adaptee;
LoginFrame_btnOK_actionAdapter(LoginFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnOK_actionPerformed(e);
}
}
}



数据库链接文件写好的
package com.finance.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.sql.*;


public class DataBase {


private static Connection con = null;
public static Connection getCon(){
try {
if (con == null) {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:finance", "sa", "sa");
}


} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return con;
}
}



...全文
2587 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
匠心零度 2010-12-11
  • 打赏
  • 举报
回复
谢谢你的提示
ypchengmsdn 2010-05-30
  • 打赏
  • 举报
回复

你应该朝着这种思路去写:

客户端 :就是一个用户名,密码的输入框。
然后用一个类去封装。然后用objecOutputStrem 将这个对象发送到服务器
端 , 得到那个类的用户名,密码,调用 你的 数据库语句,在进行判断。。。

lelehehe881212 2010-05-30
  • 打赏
  • 举报
回复
MainFrame类在哪啊?
qingzhe2008 2010-05-24
  • 打赏
  • 举报
回复
LZ,还是去看看基础的java视频教程吧。
jyfwyf 2010-05-24
  • 打赏
  • 举报
回复
能不能说的具体点,在哪里添加代码?
public boolean loginByNameAndPassword(String name,String password){
String sql = "select * from tbl_user where Name='"+name+"' and Password='"+password+"'";
try{
Connection connection = this.getCon();
PreparedStatement pstat = connection.prepareStatement(sql);
ResultSet rs = pstat.executeQuery();
if(rs.next()){
return true;
}
}catch(Exception e){
System.out.println(e.getMessage());
return false;
}
return false;
}
这个类添加到DataBase类里,然后if判断后要怎么改?具体代码是什么?
hack517 2010-05-23
  • 打赏
  • 举报
回复
你先要写一个查询是SQL语句查找用户名和密码,然后在去获得窗体中的用户名和密码,最后在进行判断。。。
java毕设 本文介绍了一款基于局域网的聊天室系统的设计和实现。该系统采用Java语言开发,使用Socket通信技术实现局域网内的实时聊天。 系统架构主要包括:客户端、服务端和数据库。客户端采用Swing技术开发,主要负责用户登陆、聊天界面展示和消息发送。服务端采用多线程技术,可以同时处理多个客户端的连接请求和消息交换。数据库使用MySQL,用于存储用户信息和聊天记录。 服务端首先启动,监听指定的端口。当有客户端连接请求时,服务端会为每个客户端创建一个新的线程进行处理。每个客户端线程负责与对应的客户端进行消息交换,并将聊天记录保存到数据库。 客户端启动后,用户需要输入用户名密码进行登陆验证。登陆成功后,客户端会显示当前在线用户列表和聊天界面。用户可以在聊天界面输入消息并发送,其他在线用户将收到该消息并显示在自己的聊天界面上。 该系统实现了用户登陆、在线用户管理、实时消息交换、聊天记录存储等功能。通过Socket技术实现了局域网内的实时多用户聊天,提高了用户体验。系统采用分层设计,结构清晰,扩展性强。 总体来说,该系统实现了基于局域网的实时多用户聊天功能,具有良好的扩展性和

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧