1、首先需要建立一个类,在这里,我命名为newlogin
newlogin类的代码如下
package p4;import java.awt.dimension;import java.awt.font;import java.awt.toolkit;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.sql.connection;import java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;import javax.swing.buttongroup;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpasswordfield;import javax.swing.jradiobutton;import javax.swing.jtextfield;public class newlogin extends jframe implements actionlistener{ private static final long serialversionuid = 1l; private connection con = null; private statement statement = null; private resultset res = null; private buttongroup buttongroup = new buttongroup(); private mypanel jp = new mypanel(); private jlabel ul = new jlabel("用户名:"); private jlabel pl = new jlabel("密 码:"); private jlabel ts = new jlabel(""); private jtextfield uname = new jtextfield(); private jpasswordfield pword = new jpasswordfield(); private jradiobutton[] butarray = { new jradiobutton("学生",true), new jradiobutton("教师") }; private jbutton login = new jbutton("登陆"); private jbutton reset = new jbutton("重置"); public newlogin() { addlistener(); initialframe(); } private void initialframe() { font font = new font("宋体",font.bold,12); setdefaultcloseoperation(jframe.exit_on_close); settitle("登陆"); jp.setlayout(null); ul.setbounds(100, 30, 60, 30); jp.add(ul); uname.setbounds(170, 30, 140, 30); jp.add(uname); pl.setbounds(100, 80, 60, 30); pword.setbounds(170, 80, 140, 30); jp.add(pl); jp.add(pword); ts.setbounds(100, 160, 200, 50); jp.add(ts); ts.setfont(font); login.setbounds(100, 220, 70, 30); jp.add(login); login.setfont(font); reset.setbounds(220, 220, 70, 30); jp.add(reset); reset.setfont(font); add(jp); setresizable(false); buttongroup.add(butarray[0]); buttongroup.add(butarray[1]); butarray[0].setbounds(120, 130, 100, 50); jp.add(butarray[0]); butarray[1].setbounds(220, 130, 100, 50); jp.add(butarray[1]); butarray[0].setcontentareafilled(false); butarray[1].setcontentareafilled(false); dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); int centerx = screensize.width/2; int centery = screensize.height/2; int w = 427; int h = 331; setbounds(centerx-w/2, centery-h/2, w, h); setvisible(true); uname.requestfocus(true); getcontentpane().add(jp); jp.getrootpane().setdefaultbutton(login); } private void addlistener() { this.login.addactionlistener(this); this.uname.addactionlistener(this); this.pword.addactionlistener(this); } @override public void actionperformed(actionevent e) { if(e.getsource() == uname) { pword.requestfocus(); } if(e.getsource() == pword) { butarray[0].requestfocus(); } if(e.getsource() == butarray[0]||e.getsource() == butarray[1]||e.getsource() == login) {// this.ts.settext("正在为您努力加载,请稍等......"); int type = this.butarray[0].isselected()?0:1; string username = this.uname.gettext().trim(); char[] p = this.pword.getpassword(); string password = string.valueof(p).trim(); if(username.equals("")) { joptionpane.showmessagedialog(this, "请输入用户名!","错误",joptionpane.error_message); ts.settext(""); return ; } if(password.equals("")) { joptionpane.showmessagedialog(this, "请输入密码!","错误",joptionpane.error_message); ts.settext(""); return ; } try { con = new connection().getconnection(); //调用自己写的一个数据库连接类 statement = con.createstatement(); if(type == 0) { string sql = "select * from stuuser where "+ "username_stu='"+username+"'and password_stu='"+password+"'"; res = statement.executequery(sql); if(res.next()) { joptionpane.showmessagedialog(this, "登录成功!","提示",joptionpane.information_message); this.dispose(); } else { joptionpane.showmessagedialog(this, "用户名或密码错误!","错误",joptionpane.error_message); ts.settext(""); uname.settext(""); pword.settext(""); } //关闭数据库连接 if(res != null) { res.close(); } if(statement != null) { statement.close(); } if(con != null) { con.close(); } } else { string sql = "select * from teauser where "+ "username_tea='"+username+"'and password_tea='"+password+"'"; res = statement.executequery(sql); if(res.next()) { string spec_name = res.getstring(1); joptionpane.showmessagedialog(this, "登录成功!","提示",joptionpane.information_message); this.dispose(); } else { joptionpane.showmessagedialog(this, "用户名或密码错误!","错误",joptionpane.error_message); ts.settext(""); uname.settext(""); pword.settext(""); } //关闭数据库连接 if(res != null) { res.close(); } if(statement != null) { statement.close(); } if(con != null) { con.close(); } } }catch(sqlexception ea) { ea.printstacktrace(); } } else if(e.getsource() == reset) { uname.settext(""); pword.settext(""); } }}
2、编写数据库连接类,在这里命名为connection,然后在类里写一个获取连接的放回,并返回一个连接。
connection类的代码如下:
package p4;import java.sql.connection;import java.sql.drivermanager;//import java.sql.resultset;import java.sql.sqlexception;//import java.sql.statement;public class connection { private connection con = null;// private statement statement = null;// private resultset res = null; string driver = "com.mysql.cj.jdbc.driver"; string url = "jdbc:mysql://localhost:3306/stu_manager?servertimezone=asia/shanghai"; string name = "root"; string passwd = "123456"; public connection() { } public connection getconnection() { try{ class.forname(driver).newinstance(); con = drivermanager.getconnection(url,name,passwd); }catch(classnotfoundexception e){ system.out.println("对不起,找不到这个driver"); e.printstacktrace(); }catch(sqlexception e){ e.printstacktrace(); }catch(exception e){ e.printstacktrace(); } return con; }}
3、在mysql数据库中需建立两个用户表,分别用来存储不同用户的登陆账号和密码,这里学生用户表为stuuser,教师用户表为teauser,建表的sql语句如下:
stuuser表:
create table stuuser( username_stu varchar(20) primary key, password_stu char(20) not null,)engine=innodb default charset=utf8;
teauser表:
create table teauser( username_tea varchar(20) primary key, password_tea char(20) not null,)engine=innodb default charset=utf8;
以上就是java如何实现简单登陆界面的详细内容。