簡單的登入系統(java+JFrame+Mysql)
阿新 • • 發佈:2018-12-11
連線資料庫
package 註冊資訊;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class conn_db{
Connection con;
String url = null;
Statement stmt;
public void connection() throws ClassNotFoundException{
//url = "jdbc:mysql://localhost:3306/wang?" +"user=root&password=wsq & useUnicode=true & characterEnunicode=UTF8";
//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08?useUnicode=true&characterEncoding=utf-8&useSSL=true","root", "wsq");
/*try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url);
System.out.println("連線成功");
stmt = con.createStatement();
}
catch(SQLException e){
e.printStackTrace();
} */
url = "jdbc:mysql://localhost:3306/wang?user=root&password=wsq&useUnicode=true&characterEncoding=UTF8";
//這裡的資料庫密碼每個人的都不同,需要更改
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url);
System.out. println("連線成功");
stmt = con.createStatement();
/*Statement s = con.createStatement();
System.out.println(con.isClosed());*/
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ClassNotFoundException {
conn_db conn = new conn_db();
conn.connection();
}
}
註冊資訊,在資料庫裡面進行檢查
package 註冊資訊;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class login_db extends conn_db implements ActionListener {
JTextField accT,nameT;
JButton okB,registB;
register re;
ResultSet rs;
public void setaccountT(JTextField a){
accT = a;
}
public void setnameT(JTextField n){
nameT = n;
}
public void setButton(JButton b1,JButton b2){
okB = b1;
registB = b2;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == okB){
if(accT.getText().equals("")) //判斷使用者輸入是否為空;
JOptionPane.showMessageDialog(null, "請填寫賬號!");
else if(nameT.getText().equals(""))
JOptionPane.showMessageDialog(null, "請輸入密碼");
else{
String accountT = accT.getText();
String namesT = nameT.getText();
try {
connection(); //載入conn_db類,連線資料庫;
boolean com = compareWithSql(accountT,namesT);
if(com)
JOptionPane.showMessageDialog(null, "登入成功");
else{
JOptionPane.showMessageDialog(null, "賬號或密碼不正確,請重新輸入");
accT.setText("");
nameT.setText("");
}
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}
else if(e.getSource() == registB){
new JFrame().dispose();
re = new register();
}
}
//賬號輸入檢查
boolean compareWithSql(String accountT,String namesT) throws Exception{
String sql;
Connection con = super.con;
Statement stmt = con.createStatement();
sql = "select * from my";
// System.out.println(sql);
rs = stmt.executeQuery(sql);
while(rs.next()){ //使用者輸入的資訊和資料庫中的資訊做比較,判斷輸入是否正確;
String acc = rs.getString(1);
String names = rs.getString(2);
if(acc.equals(accountT) && names.equals(namesT)){
//break;
return true;
}
// System.out.println(acc + " " + names);
// System.out.println(accountT + " " + namesT);
}
// System.out.println("hahahaha");
return false;
}
}
JFrame視窗登入頁面
package 註冊資訊;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.*;
public class login extends JFrame{
//JLabel accountL,nameL;
JTextField accountT,nameT;
JButton okB,registB;
Box baseB1,baseB2,box1,box2,box3; //此登入頁面採用Box佈局方式;
login_db log;
login(){
init();
}
void init(){
log = new login_db();
accountT = new JTextField(10);
nameT = new JTextField(20);
okB = new JButton("登入");
registB = new JButton("註冊");
box1 = Box.createVerticalBox();
box1.add(new JLabel("賬號:"));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel("密碼"));
box2 = Box.createVerticalBox();
box2.add(accountT);
box2.add(Box.createVerticalStrut(8));
box2.add(nameT);
box3 = Box.createHorizontalBox();
box3.add(okB);
box3.add(Box.createHorizontalStrut(20));
box3.add(registB);
baseB1 = Box.createHorizontalBox();
baseB1.add(box1);
baseB1.add(Box.createHorizontalStrut(8));
baseB1.add(box2);
baseB2 = Box.createVerticalBox();
baseB2.add(baseB1);
baseB2.add(Box.createVerticalStrut(10));
baseB2.add(box3);
okB.addActionListener(log);
registB.addActionListener(log);
log.setaccountT(accountT);
log.setnameT(nameT);
log.setButton(okB,registB);
add(baseB2);
setLayout(new FlowLayout());
setBounds(200,150,400,300);
setVisible(true);
setTitle("使用者登入介面");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
login lo = new login();
}
}
建立並檢查資料庫表
package 註冊資訊;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class register_db extends conn_db implements ActionListener{
JTextField textacc,textname;
JButton okButton,resetButton;
Statement stmt;
ResultSet rs;
// double acc;
// String name;
// Connection con = null;
public void setaccountField(JTextField a){
textacc = a;
}
public void setnameField(JTextField n){
textname = n;
}
public void setokButton(JButton b1){
okButton = b1;
}
public void setresetButton(JButton b2){
resetButton = b2;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == okButton){
if(textacc.getText().equals("")) //判斷使用者輸入是否為空;
JOptionPane.showMessageDialog(null, "請輸入賬號","警告對話方塊",JOptionPane.WARNING_MESSAGE);
else if(textname.getText().equals(""))
JOptionPane.showMessageDialog(null,"請輸入姓名","警告對話方塊",JOptionPane.WARNING_MESSAGE);
else{
String acc = textacc.getText();
String name = textname.getText();
try {
connection();
writeInSql(acc,name);
} catch (Exception e1) {
System.out.println("插入失敗");
e1.printStackTrace();
}
}
}
else if(e.getSource() == resetButton){
textacc.setText("");
textname.setText("");
}
}
void writeInSql(String acc,String name) throws Exception{
String sql;
Connection con = super.con;
Statement stmt = con.createStatement();
//建立並檢查資料庫表,若沒有"my"表,則建立表並執行插入操作,若表已存在,則直接執行操作,插入資料
sql = "create table if not exists my(account varchar(10),name varchar(20))";
stmt.executeUpdate(sql);
// System.out.println(sql); //查錯;
System.out.println("建立表成功");
//插入存文字框中獲取的資料;
sql = "insert into my(account,name) values('"+acc+"','"+name+"')";
int rw = stmt.executeUpdate(sql);
// System.out.println(sql);
if(rw <= 0){ //判斷資料是否插入成功
JOptionPane.showMessageDialog(null,"註冊失敗");
}
else{
JOptionPane.showMessageDialog(null, "註冊成功");
}
}
}
使用者頁面註冊
package 註冊資訊;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.*;
public class register extends JFrame{
JLabel accountLabel,nameLabel;
JButton okButton,resetButton;
JTextField accountText,nameText;
Box baseBox1,baseBox2, box1,box2,box3; //此註冊頁面採用Box佈局方式;
//JPanel pane1,pane2;
register_db regist;
register(){
init();
}
void init(){
setLayout(new FlowLayout());
accountLabel = new JLabel("賬號");
nameLabel= new JLabel("姓名");
accountText = new JTextField(10);
nameText = new JTextField(20);
okButton = new JButton("確定");
resetButton = new JButton("重置");
regist = new register_db();
// lab = new JLabel("使用者註冊頁面");
box1 = Box.createVerticalBox();
box1.add(accountLabel);
box1.add(Box.createVerticalStrut(8));
box1.add(nameLabel);
box2 = Box.createVerticalBox();
box2.add(accountText);
box2.add(Box.createVerticalStrut(8));
box2.add(nameText);
box3 = Box.createHorizontalBox();
box3.add(okButton);
box3.add(Box.createHorizontalStrut(15));
box3.add(resetButton);
baseBox1 = Box.createHorizontalBox();
baseBox1.add(box1);
baseBox1.add(Box.createHorizontalStrut(8));
baseBox1.add(box2);
baseBox2 = Box.createVerticalBox();
baseBox2.add(baseBox1);
baseBox2.add(Box.createVerticalStrut(10));
baseBox2.add(box3);
add(baseBox2);
okButton.addActionListener(regist);
resetButton.addActionListener(regist);
regist.setaccountField(accountText);
regist.setnameField(nameText);
regist.setokButton(okButton);
regist.setresetButton(resetButton);
setBounds(200,200,400,300);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("使用者註冊介面");
}
// void registAction(){
//
// }
// public static void main(String[] args) {
// register re = new register();
// }
}