天天看点

注册登录页面 (Java+MySQL)

    只用了5个类,包括  “连接数据库”,“注册页面”,“注册信息操作页面”,“登录页面”,“登录信息检查页面”。

注册和登录页面,我采用了BOX布局方式,因为我喜欢它的不随页面的大小改变而改变的特点,顺便通过这些练习,来检验一下自己最近的学习成果。

我发现我还是对MySQL的操作不熟,看来接下来得要有一段时间来研究MySQL数据库了。我坚持使用mysql数据库,是因为我之前对它还是了解一些的,只是没有真正上手操作,我认为数据库的操作原理应该都相似,先熟悉一门,那么别的自然是水到渠成;

行了,废话不多说了,我来分享一下我的代码,激励一下自己,如果能帮到其他和我一样的初学者,那就更好了;

(1)conn_db类

[java] view plain copy print ?

  1. package 注册信息;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6. public class conn_db{  
  7.     Connection con;  
  8.     String url = null;  
  9.     //Statement stmt;  
  10.     public void connection() throws ClassNotFoundException{  
  11.         url = "jdbc:mysql://localhost:3306/wang?"  
  12.                 + "user=root & password=883883 & useUnicode=true & characterEnunicode=UTF8";  
  13.         try{  
  14.             Class.forName("com.mysql.jdbc.Driver");  
  15.             con = DriverManager.getConnection(url);  
  16.             System.out.println("连接成功");  
  17.             //stmt = con.createStatement();  
  18.         }  
  19.         catch(SQLException e){  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23. //  public static void main(String[] args) throws ClassNotFoundException {  
  24. //      conn_db conn = new conn_db();  
  25. //      conn.connection();  
  26. //        
  27. //  }  
  28. }  
注册登录页面 (Java+MySQL)
package 注册信息;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
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=883883 & useUnicode=true & characterEnunicode=UTF8";
		try{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url);
			System.out.println("连接成功");
			//stmt = con.createStatement();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	
//	public static void main(String[] args) throws ClassNotFoundException {
//		conn_db conn = new conn_db();
//		conn.connection();
//		
//	}
}

           

(2)login类

[java] view plain copy print ?

  1. package 注册信息;  
  2. import java.awt.FlowLayout;  
  3. import java.awt.event.ActionListener;  
  4. import javax.swing.JFrame;  
  5. import javax.swing.*;  
  6. public class login extends JFrame{  
  7.     //JLabel accountL,nameL;  
  8.     JTextField accountT,nameT;  
  9.     JButton okB,registB;  
  10.     Box baseB1,baseB2,box1,box2,box3;       //此登录页面采用Box布局方式;  
  11.     login_db log;  
  12.     login(){  
  13.         init();  
  14.     }  
  15.     void init(){  
  16.         log = new login_db();  
  17.         accountT = new JTextField(10);  
  18.         nameT = new JTextField(20);  
  19.         okB = new JButton("登录");  
  20.         registB = new JButton("注册");  
  21.         box1 = Box.createVerticalBox();  
  22.         box1.add(new JLabel("账号:"));  
  23.         box1.add(Box.createVerticalStrut(8));  
  24.         box1.add(new JLabel("姓名"));  
  25.         box2 = Box.createVerticalBox();  
  26.         box2.add(accountT);  
  27.         box2.add(Box.createVerticalStrut(8));  
  28.         box2.add(nameT);  
  29.         box3 = Box.createHorizontalBox();  
  30.         box3.add(okB);  
  31.         box3.add(Box.createHorizontalStrut(20));  
  32.         box3.add(registB);  
  33.         baseB1 = Box.createHorizontalBox();  
  34.         baseB1.add(box1);  
  35.         baseB1.add(Box.createHorizontalStrut(8));  
  36.         baseB1.add(box2);  
  37.         baseB2 = Box.createVerticalBox();  
  38.         baseB2.add(baseB1);  
  39.         baseB2.add(Box.createVerticalStrut(10));  
  40.         baseB2.add(box3);  
  41.         okB.addActionListener(log);  
  42.         registB.addActionListener(log);  
  43.         log.setaccountT(accountT);  
  44.         log.setnameT(nameT);  
  45.         log.setButton(okB,registB);  
  46.         add(baseB2);  
  47.         setLayout(new FlowLayout());  
  48.         setBounds(200,150,400,300);  
  49.         setVisible(true);  
  50.         setTitle("用户登录界面");  
  51.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
  52.     }  
  53.     public static void main(String[] args) {  
  54.         login lo = new login();  
  55.     }  
  56. }  
注册登录页面 (Java+MySQL)
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();
	}
}

           

3)login_db类

[java] view plain copy print ?

  1. package 注册信息;  
  2. import java.awt.event.ActionEvent;  
  3. import java.awt.event.ActionListener;  
  4. import java.sql.Connection;  
  5. import java.sql.ResultSet;  
  6. import java.sql.Statement;  
  7. import javax.swing.*;  
  8. public class login_db extends conn_db implements ActionListener {  
  9.     JTextField accT,nameT;  
  10.     JButton okB,registB;  
  11.     register re;  
  12.     ResultSet rs;  
  13.     public void setaccountT(JTextField a){  
  14.         accT = a;  
  15.     }  
  16.     public void setnameT(JTextField n){  
  17.         nameT = n;  
  18.     }  
  19.     public void setButton(JButton b1,JButton b2){  
  20.         okB = b1;  
  21.         registB = b2;  
  22.     }  
  23.     public void actionPerformed(ActionEvent e){  
  24.         if(e.getSource() == okB){  
  25.             if(accT.getText().equals(""))           //判断用户输入是否为空;  
  26.                 JOptionPane.showMessageDialog(null, "请填写账号!");  
  27.             else if(nameT.getText().equals(""))  
  28.                 JOptionPane.showMessageDialog(null, "请输入姓名");  
  29.             else{  
  30.                 String accountT = accT.getText();  
  31.                 String namesT = nameT.getText();  
  32.                 try {  
  33.                     connection();       //加载conn_db类,连接数据库;  
  34.                     boolean com = compareWithSql(accountT,namesT);  
  35.                     if(com)  
  36.                         JOptionPane.showMessageDialog(null, "登录成功");  
  37.                     else{  
  38.                         JOptionPane.showMessageDialog(null, "账号或姓名不正确,请重新输入");  
  39.                         accT.setText("");  
  40.                         nameT.setText("");  
  41.                     }  
  42.                 }   
  43.                 catch (Exception e1) {  
  44.                     e1.printStackTrace();  
  45.                 }  
  46.             }  
  47.         }  
  48.         else if(e.getSource() == registB){  
  49.             new JFrame().dispose();  
  50.             re = new register();  
  51.         }  
  52.     }  
  53.     //用户输入检查  
  54.     boolean compareWithSql(String accountT,String namesT) throws Exception{  
  55.         String sql;       
  56.         Connection con = super.con;  
  57.         Statement stmt = con.createStatement();  
  58.         sql = "select * from my";  
  59. //      System.out.println(sql);  
  60.         rs = stmt.executeQuery(sql);  
  61.         while(rs.next()){               //用户输入的信息和数据库中的信息做比较,判断输入是否正确;  
  62.             String acc = rs.getString(1);  
  63.             String names = rs.getString(2);  
  64.             if(acc.equals(accountT) && names.equals(namesT)){  
  65.                 //break;  
  66.                 return true;  
  67.             }  
  68. //          System.out.println(acc + "   " + names);  
  69. //          System.out.println(accountT + "   " + namesT);  
  70.         }  
  71. //      System.out.println("hahahaha");  
  72.         return false;  
  73.     }  
  74. }  
注册登录页面 (Java+MySQL)
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;
		
	}
	
}

           

4)register类

[java] view plain copy print ?

  1. package 注册信息;  
  2. import javax.swing.JFrame;  
  3. import java.awt.FlowLayout;  
  4. import javax.swing.*;  
  5. public class register extends JFrame{  
  6.     JLabel accountLabel,nameLabel;  
  7.     JButton okButton,resetButton;  
  8.     JTextField accountText,nameText;  
  9.     Box baseBox1,baseBox2, box1,box2,box3;       //此注册页面采用Box布局方式;  
  10.     //JPanel pane1,pane2;  
  11.     register_db regist;  
  12.     register(){  
  13.         init();  
  14.     }  
  15.     void init(){  
  16.         setLayout(new FlowLayout());  
  17.         accountLabel = new JLabel("账号");  
  18.         nameLabel= new JLabel("姓名");  
  19.         accountText = new JTextField(10);  
  20.         nameText = new JTextField(20);  
  21.         okButton = new JButton("确定");  
  22.         resetButton = new JButton("重置");  
  23.         regist = new register_db();  
  24. //      lab = new JLabel("用户注册页面");  
  25.         box1 = Box.createVerticalBox();  
  26.         box1.add(accountLabel);  
  27.         box1.add(Box.createVerticalStrut(8));  
  28.         box1.add(nameLabel);  
  29.         box2 = Box.createVerticalBox();  
  30.         box2.add(accountText);  
  31.         box2.add(Box.createVerticalStrut(8));  
  32.         box2.add(nameText);  
  33.         box3 = Box.createHorizontalBox();  
  34.         box3.add(okButton);  
  35.         box3.add(Box.createHorizontalStrut(15));  
  36.         box3.add(resetButton);  
  37.         baseBox1 = Box.createHorizontalBox();  
  38.         baseBox1.add(box1);  
  39.         baseBox1.add(Box.createHorizontalStrut(8));  
  40.         baseBox1.add(box2);  
  41.         baseBox2 = Box.createVerticalBox();  
  42.         baseBox2.add(baseBox1);  
  43.         baseBox2.add(Box.createVerticalStrut(10));  
  44.         baseBox2.add(box3);  
  45.         add(baseBox2);  
  46.         okButton.addActionListener(regist);  
  47.         resetButton.addActionListener(regist);  
  48.         regist.setaccountField(accountText);  
  49.         regist.setnameField(nameText);  
  50.         regist.setokButton(okButton);  
  51.         regist.setresetButton(resetButton);  
  52.         setBounds(200,200,400,300);  
  53.         setVisible(true);  
  54.         setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
  55.         setTitle("用户注册界面");  
  56.     }  
  57. //  void registAction(){  
  58. //        
  59. //  }  
  60. //  public static void main(String[] args) {  
  61. //      register re = new register();  
  62. //  }  
  63. }  
注册登录页面 (Java+MySQL)
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();
//	}
	
}

           

5)register_db类

[java] view plain copy print ?

  1. package 注册信息;  
  2. import java.awt.event.ActionEvent;  
  3. import java.awt.event.ActionListener;  
  4. import java.sql.Connection;  
  5. import java.sql.ResultSet;  
  6. import java.sql.Statement;  
  7. import javax.swing.*;  
  8. public class register_db extends conn_db implements ActionListener{  
  9.     JTextField textacc,textname;  
  10.     JButton okButton,resetButton;  
  11.     Statement stmt;  
  12.     ResultSet rs;  
  13. //  double acc;  
  14. //  String name;  
  15. //  Connection con = null;  
  16.     public void setaccountField(JTextField a){  
  17.         textacc = a;  
  18.     }  
  19.     public void setnameField(JTextField n){  
  20.         textname = n;  
  21.     }  
  22.     public void setokButton(JButton b1){  
  23.         okButton = b1;  
  24.     }  
  25.     public void setresetButton(JButton b2){  
  26.         resetButton = b2;  
  27.     }  
  28.     public void actionPerformed(ActionEvent e){  
  29.         if(e.getSource() == okButton){  
  30.             if(textacc.getText().equals(""))            //判断用户输入是否为空;  
  31.                 JOptionPane.showMessageDialog(null, "请输入账号","警告对话框",JOptionPane.WARNING_MESSAGE);  
  32.             else if(textname.getText().equals(""))  
  33.                 JOptionPane.showMessageDialog(null,"请输入姓名","警告对话框",JOptionPane.WARNING_MESSAGE);  
  34.             else{  
  35.                 String acc = textacc.getText();  
  36.                 String name = textname.getText();  
  37.                 try {  
  38.                     connection();  
  39.                     writeInSql(acc,name);  
  40.                 } catch (Exception e1) {  
  41.                     System.out.println("插入失败");  
  42.                     e1.printStackTrace();  
  43.                 }  
  44.             }  
  45.         }  
  46.         else if(e.getSource() == resetButton){  
  47.             textacc.setText("");  
  48.             textname.setText("");  
  49.         }  
  50.     }  
  51.     void writeInSql(String acc,String name) throws Exception{  
  52.         String sql;  
  53.         Connection con = super.con;  
  54.         Statement stmt = con.createStatement();  
  55.         //创建并检查数据库表,若没有"my"表,则创建表并执行插入操作,若表已存在,则直接执行操作,插入数据  
  56.         sql = "create table if not exists my(account varchar(10),name varchar(20))";  
  57.         stmt.executeUpdate(sql);  
  58. //      System.out.println(sql);        //查错;  
  59.         System.out.println("创建表成功");      
  60.         //插入存文本框中获取的数据;  
  61.         sql = "insert into my(account,name) values('"+acc+"','"+name+"')";  
  62.         int rw = stmt.executeUpdate(sql);  
  63. //      System.out.println(sql);  
  64.         if(rw <= 0){             //判断数据是否插入成功  
  65.             JOptionPane.showMessageDialog(null,"注册失败");  
  66.         }  
  67.         else{  
  68.             JOptionPane.showMessageDialog(null, "注册成功");  
  69.         }  
  70.     }  
  71. }