概述
2019-07-03 19:52:32
本片代码主要有Java的桌面图形界面,Java I/O流,JDBC等等内容实现用户登录,在此地基础上添加了用户注册。代码相当的简陋,但是简单,容易理解。
数据库使用Start SampServer实现
源码:
加载数据库驱动,获取数据库连接
package 包位置
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcRe {
static {
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
}
public static void locadClass() throws ClassNotFoundException {
//加载驱动
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
}
public static Connection getConnection() throws Exception {
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
//获得连接
Connection conn = DriverManager.getConnection(url, name, passwd);
return conn;
}
public static void result(Connection conn, Statement stam) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
if (stam != null) {
try {
stam.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stam = null;
}
}
}
注册代码
package 包位置
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Register extends JFrame implements ActionListener{
JPanel p1,p2,p3,p4,p5,p6,p7,p8;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JTextField j1,j4,j5,j6,j7;
JPasswordField j2,j3;
JRadioButton r1,r2;
JCheckBox ck1,ck2,ck3,ck4;
JTextArea adddr;
JComboBox degree;
JButton bu1,bu2;
Connection conn;
Statement stam;
public Register() {
super("用户注册");
this.setLayout(new GridLayout(8,1));
p1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l1 =new JLabel("用户名:");
j1=new JTextField(18);
p1.add(l1);
p1.add(j1);
this.add(p1);
p2 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l2 =new JLabel("密码:");
j2=new JPasswordField(19);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l3 =new JLabel("确认密码:");
j3=new JPasswordField(16);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l4 =new JLabel("确性别:");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
this.r1.setSelected(true);
p4.add(l4);
p4.add(r1);
p4.add(r2);
this.add(p4);
p5 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l5 =new JLabel("爱好:");
ck1=new JCheckBox("阅读");
ck2=new JCheckBox("上网");
ck3=new JCheckBox("游泳");
ck4=new JCheckBox("旅游");
p5.add(l5);
p5.add(ck1);
p5.add(ck2);
p5.add(ck3);
p5.add(ck4);
this.add(p5);
p6 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l6 =new JLabel("地址:");
adddr =new JTextArea(2,19);
p6.add(l6);
p6.add(adddr);
this.add(p6);
p7 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l7 =new JLabel("学历:");
String str[]= {"小学","初中","高中","大学"};
degree=new JComboBox(str);
p7.add(l7);
p7.add(degree);
this.add(p7);
p8 =new JPanel(new FlowLayout(FlowLayout.CENTER));
bu1=new JButton("注册");
bu2=new JButton("取消");
p8.add(bu1);
p8.add(bu2);
this.add(p8);
//创建监听
awtEvent();
}
public static void main(String[] args) {
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听
private void awtEvent() {
//单选框互斥
r1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(r1.isSelected()){
r2.setSelected(false);
}
}
});
r2.addActionListener(new ActionListener() { <
最后
以上就是健忘斑马为你收集整理的JAVA简单实现用户登录注册的全部内容,希望文章能够帮你解决JAVA简单实现用户登录注册所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复