java简易登录工程mysql连接

来源:互联网 发布:电视频道网络直播 编辑:程序博客网 时间:2024/06/05 12:40
/**
 * 建立窗体
 * 连接数据库
 * 登录,与数据库匹配
 * 注册,向数据库插入数据
 * 
 */


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main {

static JFrame win;
static JTextField num; //账号输入框
static JTextField password; //密码输入框
static Connection conn=null;  //数据库连接
static java.sql.Statement stm=null;

public static void main(String[] args) {
 
win=new JFrame("登录");
JPanel jp=new JPanel();
num=new JTextField("请输入账号...",20);
password=new JTextField("请输入密码...",20);
JButton lg=new JButton("登录");
JButton reset=new JButton("重置");
JButton regi=new JButton("注册");
JLabel jl_num=new JLabel("账号:");
JLabel jl_password=new JLabel("密码:");

jp.add(jl_num);
jp.add(num);
jp.add(jl_password);
jp.add(password);
jp.add(lg);
jp.add(reset);
jp.add(regi);

win.add(jp);
win.setVisible(true);
win.setBounds(400,300,300,200);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//按键监听
lg.addActionListener(new Login_1());  //登录按键监听
reset.addActionListener(new Reset_1());  //重置按钮监听
regi.addActionListener(new Regi_2());  //注册按钮监听

//id输入栏鼠标监听
         num.addMouseListener(new MouseAdapter(){  
    public void mouseClicked(MouseEvent e){
num.setText("");    //点击录入框后清空提示字体
}
  });
         //password输入栏鼠标监听
         password.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
        password.setText(""); //点击录入框后清空提示字体
        }
         });
    }
}



//登录按钮事件
class Login_1 implements ActionListener{



static Connection conn=null;
static java.sql.Statement stm=null;

@Override
public void actionPerformed(ActionEvent arg0) {

//连接数据库

try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String url="jdbc:mysql://localhost:3306/students";
try {
//连接数据库
conn=DriverManager.getConnection(url, "root", "1015044485");
} catch (SQLException e) {
e.printStackTrace();
}
try {
stm=  conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//mysql连接语句
    String sql="select id,password from student_user";

try {

stm= conn.createStatement();
ResultSet rs= stm.executeQuery(sql);

boolean b=false;

//循环检索
while (rs.next()){
int id=rs.getInt(1); //数据库端返回的id
String password=rs.getString(2);  //数据库返回的密码password
//id输入框的数据
String num_1=Main.num.getText();
//转换为整型
int num_2 = Integer.parseInt(num_1);
//密码输入框的数据
String password_1=Main.password.getText();

//账号密码是否输入正确
if(num_2==id&&password_1.equals(password)){
b=true;
break;
}
else{
b=false;

}

}

if(b==true){
//登录成功提示
JOptionPane.showMessageDialog(null, "登录成功!", "提示",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("登录成功");
}

else if(b==false){
//登录失败提示
JOptionPane.showMessageDialog(null, "登录失败!", "提示",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("登录失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库关闭失败!");
}
}

}
}



//重置按钮事件
class Reset_1 implements ActionListener{


@Override
public void actionPerformed(ActionEvent arg0) {
Main.num.setText("");
Main.password.setText("");
}

}


//注册按钮事件


 class Regi_2 implements ActionListener{
 
@Override
public void actionPerformed(ActionEvent arg0) {
Nuser.N();
   Main.win.setVisible(false); //隐藏登录窗体
 }

}


//////////////regis class


/*
 * 注册代码
 */




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;






 class Nuser {


static JFrame win;
static JPanel jp;
static JLabel jl_id;
static JLabel jl_psd;
static JTextField jtf_id;
static JTextField jtf_psd;
static JButton btn_reg;
static JButton btn_cancel;


      static void N(){
  win=new JFrame("注册");
jp=new JPanel();
jl_id=new JLabel("用户名:");
jl_psd=new JLabel("密码:");
jtf_id=new JTextField(20);
jtf_psd=new JTextField(20);
btn_reg=new JButton("注册");
btn_cancel=new JButton("取消");

jp.add(jl_id);
jp.add(jtf_id);
jp.add(jl_psd);
jp.add(jtf_psd);

jp.add(btn_reg);
jp.add(btn_cancel);

win.add(jp);
win.setVisible(true);
win.setBounds(400, 300, 300, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//按键监听
btn_cancel.addActionListener(new Cancel_1());//取消按钮
btn_reg.addActionListener(new Regi_1()); //注册按钮
   }
}


 //取消按钮事件
class Cancel_1 implements ActionListener{


@Override
public void actionPerformed(ActionEvent arg0) {
Nuser.win.dispose();
Main.win.setVisible(true);
}

}


//注册按钮事件
class Regi_1 implements ActionListener{

Connection conn=null;
java.sql.Statement stm=null;

@Override
public void actionPerformed(ActionEvent e) {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}

//连接到数据库
String url="jdbc:mysql://localhost:3306/students";
try {
conn=DriverManager.getConnection(url, "root", "1015044485");
} catch (SQLException e1) {
e1.printStackTrace();
}

String Sid=Nuser.jtf_id.getText();
String Spsd=Nuser.jtf_psd.getText();
int i = Integer.parseInt(Sid);

//sql语句
String sql="insert into student_user (id,password)values("+i+","+Spsd+")";

//插入数据
try {

java.sql.Statement stm= conn.createStatement();
   stm.executeUpdate(sql);
   
   JOptionPane.showMessageDialog(null, "登录成功!", "提示",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("注册成功");
   
} catch (SQLException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "登录成功!", "提示",
JOptionPane.INFORMATION_MESSAGE);
System.out.println("注册失败");
}finally{
//关闭数据库
if(conn!=null)
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}

}

}

0 0
原创粉丝点击