您的位置:首页 > 数据库 > Oracle

JAVA GUI 登录注册(连接数据库 oracle)

2017-05-26 14:26 633 查看

本案例听过图形用户界面GUI连接oracle数据库,对数据库进行添加,查询。

源代码如下

注册界面

部分事件添加代码采用JAVA8的 Lambda

部分事件添加代码采用JAVA8的 Lambda —— Lambda详解

package interfaces;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import dao.DBUtil;
import server.User;

public class ReginInterface extends JFrame implements MouseListener,KeyListener,FocusListener{

/**
*
*/
private static final long serialVersionUID = 1L;
private Container con;
private JPanel jp;
private JButton comfirmButton;
private JButton resetButton;
private JButton gotoLoginInterface;
private JLabel [] jl;
private JTextField [] jtf;
private JRadioButton radioMan;
private JRadioButton radioWoman;
private JComboBox<String> box;
private static ReginInterface instance;
private final static String[] userKind = {"学生","教师","管理员"};
private final static String []SOURCE_DATA={"用户名:","密码:","确认密码:","邮箱:","手机号:","身份证号:","*请输入2-14个字符","*请输入6位数字"
,"*请再次输入","请输入正确的邮箱","请输入正确的手机号","请输入有效的18位身份证号"};
private ReginInterface(){
super("Regin");
this.draw();
}
public static ReginInterface getInstance(){
if(instance == null){
return new ReginInterface();
}
return instance;
}
public void draw(){
con=this.getContentPane();
jp=new JPanel();
con.add(jp,BorderLayout.NORTH);
jp.setLayout(new GridLayout(8, 3));
jl=new JLabel[13];
jtf=new JTextField[6];
for(int i=0;i<6;i++){
jl[i]=new JLabel(SOURCE_DATA[i]);
jp.add(jl[i]);
jtf[i]=new JTextField(20);
if(i==1 || i==2){
jtf[i]=new JPasswordField(20);
}
jp.add(jtf[i]);
jtf[i].addMouseListener(this);
jtf[i].addKeyListener(this);
jtf[i].addFocusListener(this);
int j=i+6;
jl[j]=new JLabel(SOURCE_DATA[j]);
jp.add(jl[j]);

}
jp.add(new JLabel("性别:"));

JPanel jp2=new JPanel();
jp.add(jp2);
jp2.setLayout(new GridLayout(1, 2));
radioMan=new JRadioButton("男",true);
radioWoman=new JRadioButton("女");
jp2.add(radioMan);
jp2.add(radioWoman);
jp.add(new JLabel());
jp.add(new JLabel("请选择用户类型:"));
box = new JComboBox<String>(userKind);
jp.add(box);

radioMan.addMouseListener(this);
radioWoman.addMouseListener(this);

JPanel jp1=new JPanel();
con.add(jp1,BorderLayout.CENTER);
comfirmButton=new JButton("确认");
comfirmButton.setEnabled(false);
jp1.add(comfirmButton);
resetButton=new JButton("重置");
jp1.add(resetButton);
gotoLoginInterface = new JButton("去登陆");
jp1.add(gotoLoginInterface);

comfirmButton.addActionListener(e -> getMessage());
resetButton.addActionListener(e -> clean());
gotoLoginInterface.addActionListener(e -> gotoLogin());

this.setLocation(500, 350);
this.setSize(700, 310);
this.setVisible(true);
}
private void getMessage() {
String userName = jtf[0].getText();
String password = jtf[1].getText();
String email = jtf[3].getText();
String telephone = jtf[4].getText();
String iDNumber = jtf[5].getText();
String sex = radioMan.isSelected()?"男":"女";
int userKind = box.getSelectedIndex();
User user_data = new User(userName, password, email, telephone, iDNumber, sex, userKind);
DBUtil.insetData(user_data);
}
private void clean() {
for(int i=0;i<6;i++){
jtf[i].setText("");
int j=i+6;
jl[j].setText(SOURCE_DATA[j]);
}
box.setSelectedIndex(0);
jtf[0].requestFocusInWindow();
}
private void gotoLogin() {
this.setVisible(false);
LoginInterface.getInstance().setVisible(true);;
}
@Override
public void mouseClicked(MouseEvent e) {

}
@Override
public void mousePressed(MouseEvent e) {
if(e.getSource().equals(radioMan)){
radioWoman.setSelected(false);
}else if(e.getSource().equals(radioWoman)){
radioMan.setSelected(false);
}
}
@Override
public void mouseReleased(MouseEvent e) {

}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {

}
private void setFocus(KeyEvent e,int i){
if((int)e.getKeyChar()==10)
jtf[i].requestFocusInWindow();//设置光标
}

@Override
public void keyTyped(KeyEvent e) {
int i=0;
while(!e.getSource().equals(jtf[i++])){
if(i==5){
break;
}
};
this.setFocus(e, i);
}
@Override
public void keyPressed(KeyEvent e) {

}
@Override
public void keyReleased(KeyEvent e) {

int i=0;
while(!e.getSource().equals(jtf[i++]));
this.checkThis(i);
this.checkAll();
}
private void checkAll(){
if( this.checkMail() && this.checkPassword() && this.confirmPassword() &&
this.checkPhoneNumber() && this.checkUserName() && this.checkIDNumber()){
this.comfirmButton.setEnabled(true);
}else{
this.comfirmButton.setEnabled(false);
}
}
private void ChangeGreenFont(JLabel jl){
jl.setForeground(Color.green);
}
private void ChangeRedFont(JLabel jl){
jl.setForeground(Color.red);
}
private void checkThis(int i){
int j=i+5;
switch (i) {
case 1:
if(checkUserName()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
case 2:
if(checkPassword()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
case 3:
if(confirmPassword()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
case 4:
if(checkMail()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
case 5:
if(checkPhoneNumber()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
case 6:
if(checkIDNumber()){
jl[j].setText("输入正确");
this.ChangeGreenFont(jl[j]);
}else{
jl[j].setText("输入错误");
this.ChangeRedFont(jl[j]);
}
break;
}
}
private boolean checkUserName(){
return jtf[0].getText().length()>=2 && jtf[0].getText().length()<=14;
}
private boolean checkPassword(){
return jtf[1].getText().matches("^[0-9]{6}$");
}
private boolean confirmPassword(){
return jtf[1].getText().equals(jtf[2].getText());
}
private boolean checkMail(){
return jtf[3].getText().matches("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");
}
private boolean checkPhoneNumber(){
return jtf[4].getText().length()==11;
}
private boolean checkIDNumber(){
return jtf[5].getText().length()==18;
}
@Override
public void focusGained(FocusEvent e) {
this.checkAll();
}
@Override
public void focusLost(FocusEvent e) {
int i=0;
while(!e.getSource().equals(jtf[i++]));
this.checkThis(i);
}
public static void main(String[] args) {
new ReginInterface();
}
}


注册界面

package interfaces;

import java.awt.Container;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import dao.DBUtil;
import server.User;

public class LoginInterface extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private Container con;
private JLabel userLab;
private JLabel passwordLab;
private JLabel userKindLab;
private JTextField user;
private JTextField password;
private JButton confirmButton;
private JButton resetButton;
private JButton gotoReginInterface;
private JComboBox<String> box;
private final static String[] userKind = {"学生","教师","管理员"};
private static LoginInterface instance = null;
private LoginInterface(){
this.setTitle("登录");
this.draw();
}
public static LoginInterface getInstance(){
if(instance == null)
return new LoginInterface();
return instance;
}
private void draw() {
con = this.getContentPane();
con.setLayout(null);

userLab = new JLabel("用户名:");
passwordLab = new JLabel("密码:");
userKindLab = new JLabel("请选择用户类型:");
userLab.setFont(new Font("宋体", Font.BOLD, 20));
passwordLab.setFont(new Font("宋体", Font.BOLD, 20));
userKindLab.setFont(new Font("宋体", Font.BOLD, 20));

user = new JTextField();
user.setFont(new Font("宋体", Font.BOLD, 20));
password = new JPasswordField();
password.setFont(new Font("宋体", Font.BOLD, 20));

confirmButton = new JButton("确认");
resetButton = new JButton("重置");
gotoReginInterface = new JButton("去注册");

con.add(user);con.add(userLab);
con.add(password);con.add(passwordLab);
con.add(confirmButton);con.add(resetButton);con.add(gotoReginInterface);

box = new JComboBox<String>(userKind);
con.add(box);con.add(userKindLab);
box.setBounds(220, 150, 200, 50);
userKindLab.setBounds(50, 150, 200, 50);

userLab.setBounds(50, 0, 100, 100);
user.setBounds(150, 20, 300, 50);

passwordLab.setBounds(50, 70, 100, 100);
password.setBounds(150, 90, 300, 50);

confirmButton.setBounds(70, 220, 100, 50);
resetButton.setBounds(220, 220, 100, 50);
gotoReginInterface.setBounds(370, 220, 100, 50);

confirmButton.addActionListener(e -> getMessage());
resetButton.addActionListener(e -> clean());
gotoReginInterface.addActionListener(e -> gotoRegin());

this.setLocation(600,400);
this.setSize(600,350);
this.setVisible(true);
}
private void getMessage() {
System.out.println("user:"+user.getText());
System.out.println("password:"+password.getText());
System.out.println("userKind:"+box.getSelectedItem());
String userName = user.getText();
String currentpassword = password.getText();
int userKind = box.getSelectedIndex();
User user_data = new User(userName, currentpassword, userKind);
System.out.println(DBUtil.hasUser(user_data));
}
private void clean() {
user.setText("");
password.setText("");
box.setSelectedIndex(0);
user.requestFocusInWindow();
}
private void gotoRegin() {
this.setVisible(false);
ReginInterface.getInstance().setVisible(true);
}
public static void main(String[] args) {
LoginInterface.getInstance();
}
}


数据库封装类

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import server.User;

public class DBUtil {
private static String dirver = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user = "scott";
private static String password = "tiger";
private static Connection con;
private static Statement stat;
private static ResultSet rs;
public static void insetData(User user_data){
try {
Class.forName(dirver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "insert into TutorManagementSystem_user values "+user_data;
try{
con = DriverManager.getConnection(url, user, password);
stat = con.createStatement();
System.out.println("影响行数"+stat.executeUpdate(sql));
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean hasUser(User user_data){
try {
Class.forName(dirver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "select user from TutorManagementSystem_user where username = '"+
user_data.getUserName()+"' and password = '"+user_data.getPassword()+
"' and userkind = "+user_data.getUserKind();
try{
con = DriverManager.getConnection(url, user, password);
stat = con.createStatement();
rs = stat.executeQuery(sql);
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}

}


数据库设计如下如

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息