简单的登录系统(java+JFrame+Mysql)
生活随笔
收集整理的這篇文章主要介紹了
简单的登录系统(java+JFrame+Mysql)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
連接數(shù)據(jù)庫
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"; //這里的數(shù)據(jù)庫密碼每個人的都不同,需要更改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(); } }注冊信息,在數(shù)據(jù)庫里面進(jìn)行檢查
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, "請?zhí)顚戀~號!");else if(nameT.getText().equals(""))JOptionPane.showMessageDialog(null, "請輸入密碼");else{String accountT = accT.getText();String namesT = nameT.getText();try {connection(); //加載conn_db類,連接數(shù)據(jù)庫;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()){ //用戶輸入的信息和數(shù)據(jù)庫中的信息做比較,判斷輸入是否正確;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();} }創(chuàng)建并檢查數(shù)據(jù)庫表
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(); //創(chuàng)建并檢查數(shù)據(jù)庫表,若沒有"my"表,則創(chuàng)建表并執(zhí)行插入操作,若表已存在,則直接執(zhí)行操作,插入數(shù)據(jù) sql = "create table if not exists my(account varchar(10),name varchar(20))"; stmt.executeUpdate(sql); // System.out.println(sql); //查錯; System.out.println("創(chuàng)建表成功"); //插入存文本框中獲取的數(shù)據(jù); sql = "insert into my(account,name) values('"+acc+"','"+name+"')"; int rw = stmt.executeUpdate(sql); // System.out.println(sql); if(rw <= 0){ //判斷數(shù)據(jù)是否插入成功 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(); // }}總結(jié)
以上是生活随笔為你收集整理的简单的登录系统(java+JFrame+Mysql)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信阳看内分泌性不孕最好的医院推荐
- 下一篇: MySQL在DOS下的基本命令操作