界面建议用可视化来做,美观且便捷.下面这个是完全用代码写的,仅供参考.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class Register extends JFrame {
JLabel jl1 = new JLabel("用户名");
JTextField jt1 = new JTextField();
JPasswordField jpw1 = new JPasswordField();
JButton register = new JButton("注册");
JButton clean = new JButton("清空");
public Register(){
add(jl1);
add(jt1);
add(jpw1);
add(register);
add(clean);
String name = jt1.getText();
String pw = jpw1.getText();
register.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","");
Statement sta = con.createStatement();
sta.executeUpdate("INSERT INTO register VALUES(name,email,pw)");
JOptionPane.showMessageDialog(null,"注册成功","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception ex){
ex.getStackTrace();
});
clean.addActionListener(new ActionListener(){
jt1.setText("");
jpw1.setText("");
public static void main(String[] args){
Register frame = new Register();
frame.setTitle("用户注册");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
效果图
代码
!DOCTYPE?html
html
head
title先锋图书馆管理系统-登录/title
style
*{
margin:?0;
padding:?0;
list-style:?none;
#top{
width:?1000px;
margin:?0?auto;
#top_top{
background:?deepskyblue;
#top_top_left{
float:?left;
#top_top_leftlabel{
color:?white;
float:?right;
padding-left:?10px;
#top_bottom{
#top_bottom_left{
background:?skyblue;
#top_bottom_right{
color:?blueviolet;
text-align:?center;
background:?lightskyblue;
#content{
#content#text{
padding-top:?100px;
font-family:"楷体";
font-weight:?bold;
#content#login{
#content#loginimg{
#content#login#select{
#content#login#select#d1{
#content#login#selectp{
#bottom{
/style
/head
body
form?id="select"
p
input?type="radio"?name="user"?value="read"/读者nbsp;nbsp;nbsp;nbsp;
input?type="radio"?name="user"?value="admin"/管理员
/pbr/
/p
/form
/body
script?type="text/javascript"?src="JQuery/jquery.js"/script
script?type="text/javascript"?src="js/GetCurrentTime.js"/script
script
//验证用户名和密码
function?put(){
var?name?=?d[0].value;
var?pass?=?d[1].value;
var?user?=?null;
var?r?=?document.getElementsByName("user");//获取用户类型
for(i=0;ir.length;i++){
if(r[i].checked){
user=r[i].value;
//console.log(name?+?","?+pass?+?","?+user);//输出测试
if(user==null){
window.alert("请选择用户类型!");
}else?if(user=="admin"?name!="admin"){
window.alter("用户名错误!");
window.alert("密码错误!");
}else{
window.alert("用户名错误");
/script
/html
public class User{
//定义私有属性 ?用户名和密码
private String userName;
private String password;
public User(String userName,String password){
this.userName=userName;
this.password=password;
//私有属性的set ?get 方法
public void setUserName(String userName){
? this.userName=userName;
public void setPassword(String password){
? this.password=password; ?
public String getUserName(){
return this.userName;
public String getPassword(){
return this.password;
//用来判断用户名和密码是否正确
public boolean panDuan(){
if("用户名".equals(this.userName)"密码".equals(this.password)){
? ? ? ?System.out.println("登入成功!");
? ? ? return true;
? ?}else{
? ? ? System.out.println("登入失败!");
? ? ?return false;
? ?}
public static void main (String[] args){
System.out.println("请输入用户名:");
scanner sc=new scanner(System.in);
String userName=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
User u=new User(userName,password);
u.panDuan();
补充:Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言.Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群.
String username = "",password = "",passwordagain = ""; // 定义用户名和密码
将该变量等于为全局变量 或局部变量即可
//这个是我写的,里面有连接数据库的部分.你可以拿去参考一下
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用户名:");
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("确定");
public LoginFrm() {
this.setTitle("登陆");
JPanel jp = (JPanel) this.getContentPane();
jp.add(lbl1);
jp.add(txt);
jp.add(pf);
jp.add(btn1);
btn1.addActionListener(this);
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName("com.mysql.jdbc.Driver");// mysql数据库
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/Car_zl", "root", "1");// 数据库名为Car_zl,密码为1
System.out.println("com : "+ con);
Statement cmd = con.createStatement();
String sql = "select * from user where User_ID='"
+ txt.getText() + "' and User_ps='"
+ pf.getText() + "'" ;
ResultSet rs = cmd
.executeQuery(sql);// 表名为user,user_ID和User_ps是存放用户名和密码的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, "登陆成功!");
} else
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
} catch (Exception ex) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frm.setVisible(true);
以上就是土嘎嘎小编为大家整理的java账号密码注册代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!