java swing 连接mysql_java连接数据库并登录 swing的

代码如下:packagecom.ts;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.*;importjavax.swing.*;publicclassLoginextends...

代码如下:

package com.ts;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.*;

import javax.swing.*;

public class Login extends JFrame {

private JButton esc =new JButton("取消");

private JLabel user=new JLabel("用户名");

private JLabel pass=new JLabel("密 码");

private JTextField txtUser=new JTextField(10);

JPasswordField txtPass=new JPasswordField(10);

JButton ok=new JButton("确定");

private static String userStr;

Statement stem=null;

static Connection con=null;

ResultSet res=null;

Login(){

this.setTitle("登录系统");

this.show(true);

this.setSize(200, 100);

JPanel panel1=new JPanel();

JPanel panel2=new JPanel();

JPanel panel3=new JPanel();

panel1.add(user);

panel1.add(txtUser);

panel2.add(pass);

panel2.add(txtPass);

panel3.add(ok);

panel3.add(esc);

this.add(panel1,BorderLayout.NORTH);

this.add(panel2,BorderLayout.CENTER);

this.add(panel3,BorderLayout.SOUTH);

Toolkit kit=Toolkit.getDefaultToolkit();

Dimension screenSize=kit.getScreenSize();

int screenWidth=screenSize.width;

int screenHigth=screenSize.height;

this.setLocation(screenWidth/3, screenHigth/4);

this.setResizable(false);

this.pack(); //使窗体适应大小

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

esc.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

System.exit(1);

}

});

//登陆事件

ok.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

try {

//加载数据库

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

con=DriverManager.getConnection("jdbc:sqlserver://localhost:1434;databaseName=Info","sa","111111111");

userStr = txtUser.getText();

res = stem.executeQuery("select * from addInfo where id='"

+ userStr + "' and pass='" + pass + "'");

if(txtUser.getText()==txtUser ){ //这里该怎么写啊

JOptionPane.showMessageDialog(null, " 登陆成功","提示",JOptionPane.INFORMATION_MESSAGE);

}

else{

JOptionPane.showMessageDialog(null, " 登陆失败","提示",JOptionPane.INFORMATION_MESSAGE);

}

}

catch (Exception e2) {

sqlClose();

}

}

});

}

public void sqlClose() {// 关闭连接

try {

con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new Login();

}

}

求高手

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值