分三個(gè)包,自己建個(gè)包,導(dǎo)進(jìn)去就ok了,數(shù)據(jù)庫(kù)是access的。
目前創(chuàng)新互聯(lián)建站已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、白山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
package 登錄;
import java.awt.EventQueue;
public class Cilent {
private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cilent window = new Cilent();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cilent() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("登陸界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
JLabel lblNewLabel = new JLabel("用戶名");
lblNewLabel.setBounds(38, 43, 80, 34);
frame.getContentPane().add(lblNewLabel);
textField = new JTextField();
textField.setBounds(155, 42, 227, 37);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label = new JLabel("密 碼");
label.setBounds(38, 115, 80, 34);
frame.getContentPane().add(label);
passwordField = new JPasswordField();
passwordField.setBounds(155, 115, 227, 37);
frame.getContentPane().add(passwordField);
JButton btnNewButton = new JButton("登 錄");
btnNewButton.setBounds(60, 187, 115, 34);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
UserCheck UC=new UserCheck(textField.getText(),String.valueOf(passwordField.getPassword()));
if(UC.getI()!=0) //有此用戶
{
frame.setVisible(false);
}
else
{
textField.setText("");
passwordField.setText("");
}
}
});
JButton button = new JButton("取 消");
button.setBounds(242, 187, 115, 34);
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
textField.setText("");
passwordField.setText("");
}
});
}
}
/*****************************************************************/
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;
import 操作處理.UsersCL;
/**@author 20111024
* 檢測(cè)登錄的用戶在數(shù)據(jù)庫(kù)中有無(wú),若沒(méi)有,則提示沒(méi)有此用戶,
* 若有,則判斷級(jí)別:普通用戶還是管理員。
*/
public class UserCheck {
private int i=0; //用戶級(jí)別:0不是用戶、1是管理員、2是普通用戶
UserCheck(String name ,String password)
{
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
String query="select * from users where name='"+name+"' and passwd='"+password+"'";
rs=stmt.executeQuery(query);
if(rs.next())
{
//數(shù)據(jù)庫(kù)中有此用戶,訪問(wèn)成功
i=Integer.parseInt(rs.getString(3));
UsersCL UL=new UsersCL(i);
}
else
{
i=0; //沒(méi)有用戶是默認(rèn)是0級(jí)
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getI() {
return i;
}
}
/********************************************************************************************/
package 操作處理;
import java.awt.EventQueue;
public class UsersCL implements ActionListener{
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private int i=0;
private JLabel label_3;
private JTextField textField_4;
public UsersCL(int i) {
this.i=i;
frame = new JFrame();
frame.setTitle("用戶處理界面");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
frame.setVisible(true);
JLabel lblNewLabel = new JLabel("學(xué) 號(hào)");
lblNewLabel.setBounds(24, 32, 74, 29);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("姓 名");
label.setBounds(24, 71, 74, 29);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("年 齡");
label_1.setBounds(24, 110, 74, 29);
frame.getContentPane().add(label_1);
label_3 = new JLabel("性 別");
label_3.setBounds(24, 149, 74, 29);
frame.getContentPane().add(label_3);
JLabel label_2 = new JLabel("狀 態(tài)");
label_2.setBounds(24, 195, 74, 29);
frame.getContentPane().add(label_2);
textField = new JTextField();
textField.setBounds(101, 34, 113, 25);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(101, 73, 113, 25);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(101, 112, 113, 25);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setEditable(false);
textField_3.setColumns(10);
textField_3.setBounds(101, 199, 288, 25);
frame.getContentPane().add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(101, 149, 113, 25);
frame.getContentPane().add(textField_4);
if(1==i)
{
JButton btnNewButton = new JButton("追 加");
btnNewButton.setBounds(276, 41, 113, 29);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
btnNewButton.setActionCommand("追加");
JButton button_1 = new JButton("刪 除");
button_1.setBounds(276, 145, 113, 29);
frame.getContentPane().add(button_1);
button_1.addActionListener(this);
button_1.setActionCommand("刪除");
}
JButton button = new JButton("查 詢");
button.setBounds(276, 91, 113, 29);
frame.getContentPane().add(button);
button.addActionListener(this);
button.setActionCommand("查詢");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name,age,sex,query=null;
int num,age1,count=0;
num=Integer.parseInt(textField.getText());
name=textField_1.getText();
age1=Integer.parseInt(textField_2.getText());
sex=textField_4.getText();
if(e.getActionCommand().equals("追加"))
{
query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";
count=1;
}
else if(e.getActionCommand().equals("查詢"))
{
query="select * from students where XSB="+num+";";
count=2;
}
else if(e.getActionCommand().equals("刪除"))
{
query="delete from students where XSB="+num+" and name="+"'"+name+"'";
count=3;
}
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
String jdriver="sun.jdbc.odbc.JdbcOdbcDriver";
String connectDB="jdbc:odbc:Students";
String query1=null;
try {
Class.forName(jdriver);
con=DriverManager.getConnection(connectDB);
stmt=con.createStatement();
if(count==1)
{
query1="select * from students where XSB="+num+";";
rs=stmt.executeQuery(query1);
if(rs.next())
textField_3.setText("已經(jīng)由此記錄,不能追加!");
else
{
stmt.executeUpdate(query);
textField_3.setText("已經(jīng)追加完成!");
}
}
else if(2==count)
{
stmt.executeQuery(query);
rs=stmt.executeQuery(query);
if(rs.next())
{
textField_3.setText("已查找到此記錄!");
}
else
{
textField_3.setText("沒(méi)有此記錄,可以追加!");
}
}
else if(3==count)
{
query1="select * from students where XSB="+num+" and name="+"'"+name+"'";
rs=stmt.executeQuery(query1);
if(rs.next())
{
stmt.executeUpdate(query);
textField_3.setText("已刪除此記錄!");
}
else
textField_3.setText("無(wú)此記錄!");
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
//關(guān)閉資源
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (Exception e2) {
// TODO: handle exception
}
con=null;
}
}
}
}
這個(gè)在數(shù)據(jù)庫(kù)中增加一個(gè)int 型字段就可以了。每次登錄都先查詢?cè)撟侄稳绻∮谌图右?,?dāng)?shù)谒拇蔚卿洉r(shí),改值為4,查到大于三就彈出錯(cuò)誤提示,禁止登錄。
效果圖
代碼
!DOCTYPE?html
html
head
meta?charset="UTF-8"
title先鋒圖書(shū)館管理系統(tǒng)-登錄/title
style
*{
margin:?0;
padding:?0;
list-style:?none;
}
#top{
width:?1000px;
height:?95px;
margin:?0?auto;
margin-top:?25px;
}
#top_top{
width:?1000px;
height:?65px;
background:?deepskyblue;
}
#top_top_left{
width:?300px;
height:?65px;
float:?left;
}
#top_top_leftlabel{
width:?200px;
height:?65px;
color:?white;
float:?right;
}
#top_top_left#a2{
padding-left:?10px;
padding-top:?20px;
font-size:?16px;
}
#top_bottom{
width:?1000px;
height:?30px;
}
#top_bottom_left{
width:?340px;
height:?30px;
line-height:?30px;
font-size:?12px;
background:?skyblue;
color:?white;
text-indent:?2em;
float:?left;
}
#top_bottom_right{
width:?660px;
height:?30px;
line-height:?30px;
font-size:?12px;
color:?blueviolet;
text-align:?center;
float:?right;
background:?lightskyblue;
}
#content{
width:?1000px;
height:?600px;
margin:?0?auto;
background:#587FBA;
}
#content#text{
width:?1000px;
height:?50px;
line-height:?50px;
padding-top:?100px;
font-size:?36px;
font-family:"楷體";
font-weight:?bold;
text-align:?center;
}
#content#login{
width:?480px;
height:?210px;
margin-top:?20px;
margin-left:?260px;
background:?#85A0CB;
}
#content#loginimg{
float:?left;
}
#content#login#select{
width:?305px;
height:?210px;
float:?right;
}
#content#login#selectdiv{
width:?230;
height:?30px;
margin-left:?30px;
}
#content#login#select#d1{
margin-top:30px;
margin-bottom:?3px;
}
#content#login#selectp{
font-size:?14px;
margin-left:?95px;
}
#bottom{
width:?1000px;
height:?35px;
line-height:?35px;
margin:?0?auto;
background:?deepskyblue;
text-align:?center;
color:?white;
}
/style
/head
body
div?id="top"
div?id="top_top"
div?id="top_top_left"
img?src="img/test/a13.png"?width="78px"?height="65px"label?id="a2"先鋒圖書(shū)館系統(tǒng)管理平臺(tái)/label
/div
/div
div?id="top_bottom"
div?id="top_bottom_left"當(dāng)前位置?:?首頁(yè)??系統(tǒng)管理??登錄/div
div?id="top_bottom_right"當(dāng)前時(shí)間?:?label?id="lable"/label/div
/div
/div
div?id="content"
div?id="text"歡迎登錄先鋒圖書(shū)館管理系統(tǒng)/div
div?id="login"
img?src="img/test/a14.png"??width="175px"?height="210px"/
form?id="select"
div?id="d1"用戶名:?nbsp;nbsp;input?type="text"?//div
div密????nbsp;?碼:?nbsp;nbsp;input?type="password"?//div
p
input?type="radio"?name="user"?value="read"/讀者nbsp;nbsp;nbsp;nbsp;
input?type="radio"?name="user"?value="admin"/管理員
/pbr/
p
input?type="button"?value="確定"?style="width:?50px;"?onclick="put()"/nbsp;nbsp;nbsp;nbsp;
input?type="reset"?value="重置"?style="width:?50px;"/
/p
/form
/div
/div
div?id="bottom"欣欣科技有限公司版權(quán)所有/div
/body
script?type="text/javascript"?src="JQuery/jquery.js"/script
script?type="text/javascript"?src="js/GetCurrentTime.js"/script
script
//驗(yàn)證用戶名和密碼
function?put(){
var?d?=?$("#selectdivinput");//獲取用戶名和密碼
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);//輸出測(cè)試
if(user==null){
window.alert("請(qǐng)選擇用戶類型!");
}else?if(user=="admin"??name!="admin"){
window.alter("用戶名錯(cuò)誤!");
}else?if(user=="admin"??name=="admin"??pass!="123456"){
window.alert("密碼錯(cuò)誤!");
}else?if(name=="admin"??pass=="123456"??user=="admin"){
window.location.href="work_02_welcome.html";//在js中在本頁(yè)面中打開(kāi)新鏈接
}else{
window.alert("用戶名錯(cuò)誤");
}
}
/script
/html
你這個(gè)登陸程序哪來(lái)的修改按鈕啊,只有確定和退出兩個(gè)按鈕嘛.
你這登陸用戶名是:sdd 密碼是:123 而且要程序中已經(jīng)定死了.這一句:if ((name.equals("sdd")) (passWord.equals("123")))說(shuō)的.
你如果想添加一個(gè)修改用戶名和密碼的按鈕就必須用一個(gè)另外的文件進(jìn)行保存,當(dāng)然用數(shù)據(jù)庫(kù)就更好了.而你這個(gè)沒(méi)必要改,因?yàn)闆](méi)有永久存貯的文件,即使你改了密碼,當(dāng)你下次再運(yùn)行時(shí)密碼又被重新初始化了.
弱密碼校驗(yàn),需要用到數(shù)據(jù)庫(kù)表,用戶表,用戶登錄日志表,用戶每次登錄系統(tǒng)的時(shí)候可以先查一下用戶日志表,如果沒(méi)有返回結(jié)果,說(shuō)明沒(méi)有登錄過(guò),這就是所謂的首次登錄。或者還要另外一種方法,用戶表中可以有一個(gè)status字段,用戶注冊(cè)后該字段的值默認(rèn)為0,只有在登錄成功后再置為1。這樣,首次登錄該字段值位0,程序可以判斷,只要改值是0,就強(qiáng)制修改密碼
package test;
import java.util.Scanner;
public class TestLogin {
private static final String USERNAME = "Tom";//此處定義用戶名
private static final String PASSWORD = "123";//定義密碼
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請(qǐng)輸入您的用戶名:");
String username_in = sc.next();
System.out.print("請(qǐng)輸入您的密碼:");
String password_in = sc.next();
if(username_in.equals(USERNAME) password_in.equals(PASSWORD)){
System.out.println("登錄成功");
}else{
System.out.println("用戶名或密碼錯(cuò)誤");
}
}
}
這是從控制臺(tái)輸入的最簡(jiǎn)單的方法。