java簡易登錄工程mysql連接

/**
 * 建立窗體
 * 連接數據庫
 * 登錄,與數據庫匹配
 * 註冊,向數據庫插入數據
 * 
 */


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();
}
}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章