java登錄與注冊
String username = "",password = "",passwordagain = ""; // 定義用戶名和密碼
將該變數等於為全局變數 或局部變數即可
B. 濡備綍娉ㄥ唽java紼嬪簭涓簑indows鏈嶅姟
榪欓噷浠嬬粛涓嬪備綍鍒╃敤javaService 杞浠舵妸java 紼嬪簭娉ㄥ唽涓簑indows 鏈嶅姟銆
涓銆 鍒╃敤javaService 娉ㄥ唽java 紼嬪簭涓簑indows 鏈嶅姟
[1] 涓嬭澆javaService
璁塊棶緗戝潃http://javaservice.objectweb.org/ 涓嬭澆windows 鐗堟湰鐨刯avaService 鏂囦歡錛屾垜涓嬭澆鐨勬槸JavaService-2.0.10.rar 錛岀洰鍓嶆渶鏂扮殑鐗堟湰灝辨槸鈥2.0.10 鈥濄
[2] 瀹夎卝avaService
瑙e帇鎴戜滑涓嬭澆涓嬫潵鐨刯avaServices 鍒頒竴涓鐩褰曪紝鎴戞槸瑙e帇鍒扮洰褰曗淒:/software/JavaService-2.0.10 鈥濅笅錛堣В鍘嬪埌浠諱綍鐩褰曢兘鍙浠ワ紝鏈濂藉埆瑙e帇鍒頒腑鏂囩洰褰曪紝鐪佺殑鍑虹幇闂棰 錛
[3] 緙栧啓瀹氭椂鍏蟲満浠g爜錛岃佺浜岀珷鐨勫畾鏃跺叧鏈轟唬鐮
1) 鍏蜂綋浠g爜鍙傚姞絎浜岀珷錛岀被鐨勫悕瀛椾負錛
com.test.timer.TimerShutDownWindows
2) 鎶婄紪鍐欏悗鐨刯ava 鏂囦歡瀵煎嚭涓篶lass 鐨勫艦寮忥紝鎶婂煎嚭鐨勭被鏀懼埌鐩褰曗淒:/software/JavaService-2.0.10/classes/com/test/timer 鈥濅笅銆備篃灝辨槸鎶婂煎嚭鐨刢om 鍖呮斁鍒
鈥淒:/software/JavaService-2.0.10/classes鈥 鐩褰曚笅銆
[4] 娉ㄥ唽java 紼嬪簭涓簑indows 鏈嶅姟
榪涘叆鈥淒:/software/JavaService-2.0.10 鈥滅洰褰曪紝鎵ц屽備笅鍛戒護錛
JavaService.exe -install MyShutDownService "%JAVA_HOME%"/jre/bin/server/jvm.dll -Djava.class.path="%JAVA_HOME%"/lib/tools.jar;D:/software/JavaService-2.0.10/classes -start com.test.timer.TimerShutDownWindows
鍏朵腑鈥-install 鈥滃悗闈㈢殑鍙傛暟涓烘湇鍔$殑鍚嶇О錛屸-start 鈥濆弬鏁板悗杈規槸瑕佸惎鍔ㄧ殑綾誨悕錛屸淒java.class.path 鈥濆悗闈㈠弬鏁頒腑鐨
鈥淒:/software/JavaService-2.0.10/classe 鈥濆湴鍧鏄鎴戠殑鈥淭imerShutDownWindows 鈥濈被瀛樻斁鐨勮礬寰勶紝瀹為檯搴旂敤涓淇鏀逛負鑷宸辯殑classPath 鍗沖彲銆
榪欓噷闇瑕佹敞鎰忓嚑鐐癸細
1) 鈥%JAVA_HOME% 鈥漥dk 鐩褰曪紝濡傛灉娌℃湁閰嶇疆jdk 鐩褰曪紝鍒欐浛鎹涓簀dk 鐨勫疄闄呯粷瀵瑰湴鍧銆
2) -Djava.class.path 鏄蹇呴』鐨勶紝鍥犱負鏈嶅姟鍚鍔ㄧ殑鏃跺欐棤娉曡塊棶緋葷粺鐨凜LASSPATH 鍙橀噺錛屾墍浠ュ繀欏誨湪榪欓噷澹版槑錛涘傛灉jar 姣旇緝澶氾紝涓洪伩鍏嶅啓鐨勫懡浠よ繃闀匡紝鎴戜滑鍙浠ヤ嬌鐢ㄢ-Djava.ext.dirs=jars 鎵鍦ㄧ洰褰曗濆弬鏁般
3) 鏈嶅姟娣誨姞涔嬪悗錛屽彲浠ュ湪鍛戒護琛屼腑鏁插叆鈥渟ervices.msc 鈥濆懡浠ゆ潵鏌ョ湅鎵鏈夋湇鍔★紝騫跺彲浠ュ規湇鍔$殑鍚鍔ㄧ被鍨嬶紙鑷鍔ㄥ惎鍔ㄨ繕鏄鎵嬪姩鍚鍔ㄧ瓑錛夎繘琛屼慨鏀廣
[5] 嫻嬭瘯
1) 鍚鍔ㄦ湇鍔
褰撴垜浠娉ㄥ唽瀹屾湇鍔″悗錛屾垜浠鍙浠ラ氳繃鍛戒護鈥渘et start MyShutDownService 鈥濇潵鍚鍔ㄨユ湇鍔★紝鏈嶅姟鍚鍔ㄥ悗浼氬湪D 鐩樻牴鐩褰曠敓鎴恗y_shutdown.log 鏃ュ織鏂囦歡銆
2) 鍏抽棴鏈嶅姟
濡傛灉鎴戜滑瑕佸叧闂鏈嶅姟錛屽彲浠ラ氳繃鍛戒護鈥渘et stop MyShutDownService 鈥濇潵鍏抽棴璇ユ湇鍔°
3) 鍒犻櫎鏈嶅姟
褰撴垜浠鎯沖垹闄よユ湇鍔℃椂錛屽彲浠ヤ嬌鐢ㄥ懡浠も渟c delete MyShutDownService 鈥濇潵鍒犻櫎璇ユ湇鍔°
浜屻 瀹氭椂鍏蟲満浠g爜
packagecom.test.timer;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
{
/*媯嫻嬫槸鍚﹂渶瑕佸叧鏈虹殑鏃墮棿闂撮殧*/
privatestaticlongm_nDetectInterval=5000;
/*璁板綍涓婃℃嫻嬬殑鏃墮棿錛屼互姣縐掍負鍗曚綅*/
privatestaticlongm_lLastMilliSeconds=0;
/*鍙浠ヤ嬌鐢ㄧ數鑴戠殑鏈灝忓皬鏃*/
privatestaticintm_nUsePCMinHour=17;
/*鍙浠ヤ嬌鐢ㄧ數鑴戠殑鏈澶у皬鏃*/
privatestaticintm_nUseComputerMaxHour=23;
/*濡傛灉鍒嗛挓瓚呰繃榪欎釜鏃墮棿錛屽垯鍏蟲満璁$畻鏈*/
privatestaticintm_nMinutes=25;
/*璁板綍鏃ュ織鐨勬枃浠剁殑淇濆瓨浣嶇疆*/
privatestaticStringm_sLogFile="D:"+File.separator
+"my_shutdown.log";
/*璁板綍褰撳墠緋葷粺鏄鍚﹀凡緇忓惎鍔ㄨ嚜鍔ㄥ叧闂紼嬪簭*/
=false;
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//1.鍗曠嫭寮鍚涓涓綰跨▼鍘繪嫻
ThreadaThread=newThread(newTimerDetector());
aThread.start();
}
/**
*瀹氫箟鍐呴儴綾
*
*@authorAdministrator
*
*/
{
/*
*(non-Javadoc)
*
*@seejava.lang.Runnable#run()
*/
publicvoidrun(){
//1.鑾峰彇鏃ュ織鏂囦歡
PrintWriterout=null;
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
try{
out=newPrintWriter(newFileWriter(m_sLogFile,true),true);
}catch(IOExceptione1){
out=null;
e1.printStackTrace();
}
//2.璁板綍鏈嶅姟鍚鍔ㄦ椂闂
appendLog(out,"鏈嶅姟鍚鍔ㄦ椂闂達細"+df.format(newDate()));
while(true){
//1.鍒ゆ柇褰撳墠緋葷粺鏃墮棿鏄鍚﹁淇鏀硅繃
booleanbShoudShutDownPC=validateShoudShutDownPC(out);
if(bShoudShutDownPC){
//楠岃瘉娌¢氳繃錛屽己鍒跺叧鏈
exectueShutDown(out);
}else{
bHasShutDownPC=false;
}
//2.褰撳墠綰跨▼浼戠湢涓
try{
Thread.sleep(m_nDetectInterval);
}catch(InterruptedExceptione){
appendLog(out,e.getMessage());
}
}
}
/**
*楠岃瘉褰撳墠鏃墮棿鏄鍚︽槸闇瑕佸叧鏈虹殑鏃墮棿
*
*@return
*/
(PrintWriter_out){
//1.鍒ゆ柇鏄鍚︿慨鏀逛簡緋葷粺鏃墮棿
booleanbHasModifySystemTime=detectModifySytemTime(_out);
appendLog(_out,"bHasModifySystemTime錛"+bHasModifySystemTime);
if(bHasModifySystemTime){
returnbHasModifySystemTime;
}
//2.娌℃湁淇鏀圭郴緇熸椂闂達紝鍒欏垽鏂褰撳墠鏃墮棿鏄鍚﹁秴榪囦簡鎸囧畾鐨勬椂闂
booleanbShoudSleep=nowIsSleepTime();
appendLog(_out,"bShoudSleep錛"+bShoudSleep);
if(bShoudSleep){
returnbShoudSleep;
}
returnfalse;
}
/**
*鍒ゆ柇褰撳墠鏃墮棿鏄鍚﹀簲璇ヤ紤鎮鐨勬椂闂
*
*@return
*/
privatebooleannowIsSleepTime(){
//1.鑾峰彇褰撳墠灝忔椂鍜屽垎閽
CalendaraCalendar=Calendar.getInstance();
intnHour=aCalendar.get(Calendar.HOUR_OF_DAY);
intnMinute=aCalendar.get(Calendar.MINUTE);
//2.鍒ゆ柇褰撳墠灝忔椂鏄鍚﹀湪鍙浠ヤ嬌鐢≒C鐨勬椂闂村唴,鏈澶у皬鏃朵負23
if(nHour<m_nUsePCMinHour){
returntrue;
}
//23鐐歸渶瑕佸崟鐙鍒ゆ柇錛岃秴榪23鐐30灝卞簲璇ヤ紤鎮
if((nHour>=m_nUseComputerMaxHour)&&(nMinute>=m_nMinutes)){
returntrue;
}
//3.闈炰紤鎮鏃墮棿
returnfalse;
}
/**
*鍒ゆ柇鏄鍚︽湁浜轟慨鏀逛簡緋葷粺鏃墮棿錛屽傛灉鏈変漢淇鏀逛簡緋葷粺鏃墮棿榪斿洖true錛<BR>
*鍚﹀垯榪斿洖false
*
*@return
*/
(PrintWriter_out){
//1.絎涓嬈℃嫻嬬郴緇熸椂闂
if(m_lLastMilliSeconds==0){
m_lLastMilliSeconds=System.currentTimeMillis();
returnfalse;
}
//2.媯嫻嬩袱嬈℃椂闂寸殑宸鍊
longlInteral=System.currentTimeMillis()-m_lLastMilliSeconds;
lInteral=Math.abs(lInteral);
//3.鍒ゆ柇涓ゆ$殑鏃墮棿闂撮殧,涓ゆ$粨鏋滀笉涓瀹氬畬鍏ㄧ瓑浜m_nDetectInterval錛屽厑璁歌宸涓1鍒嗛挓
longlMaxInterval=m_nDetectInterval+60*1000;
appendLog(_out,"lInteral錛::"+lInteral);
appendLog(_out,"lMaxInterval錛::"+lMaxInterval);
if(lInteral>lMaxInterval){
//鏈変漢淇鏀逛簡緋葷粺鏃墮棿錛屽己鍒跺叧鏈
returntrue;
}
//4.鍙鏈夋病浜轟慨鏀規椂闂存墠璁板綍涓婃℃嫻嬫椂闂
m_lLastMilliSeconds=System.currentTimeMillis();
returnfalse;
}
/**
*鍦ㄦ寚瀹氱殑嫻佷腑鍐欏叆鏃ュ織淇℃伅
*
*@param_outWriter
*@param_sAppendContent
*/
privatevoidappendLog(PrintWriter_outWriter,String_sAppendContent){
if(_outWriter==null){
return;
}
_outWriter.println(_sAppendContent);
}
/**
*鎵ц屽叧鏈哄懡浠
*/
privatevoidexectueShutDown(PrintWriter_out){
if(bHasShutDownPC){
SimpleDateFormatdf=newSimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");
appendLog(_out,"緋葷粺鍗沖皢鍏抽棴,褰撳墠鏃墮棿錛"+df.format(newDate()));
return;
}
appendLog(_out,"鏈変漢淇鏀逛簡緋葷粺鏃墮棿錛岀郴緇熷己鍒跺叧鏈猴紒");
//鍏蟲満
try{
Runtime.getRuntime().exec(
"shutdown-s-t120-c/"寰堟櫄浜嗭紝璇ョ潯瑙変簡,2鍒嗛挓鍚庡叧闂璁$畻鏈恆/"");
}catch(IOExceptione){
appendLog(_out,e.getMessage());
}
bHasShutDownPC=true;
}
}
}
C. java用戶登錄注冊模塊最核心技術點是什麼
登錄就是把用戶名和密碼提交到Java後台,然後查詢資料庫有沒有這一條記錄,有的話登錄成功,沒有說明用戶名或者密碼錯誤
D. java語言實現用戶注冊和登錄
//這個是我寫的,裡面有連接資料庫的部分。你可以拿去參考一下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel("用戶名:");
JLabel lbl2 = new JLabel("密碼:");
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton("確定");
JButton btn2 = new JButton("取消");
public LoginFrm() {
this.setTitle("登陸");
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.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) {
}
if (ae.getSource() == btn2) {
System.out.println("1111111111111");
//txt.setText("");
//pf.setText("");
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
E. java使用面向對象寫一個登錄注冊查詢的功能
import java.util.Scanner;/**
* 採用面向對象的方式 寫一個登錄系統
* @author Administrator
*
*///用戶信息class UserInfo{ public static String[] user = new String[10]; public static String[] passwd = new String[10];
public UserInfo() { this.user[0] = "test"; this.passwd[0] ="123456";
}
}//找回密碼class ZhaoHui extends UserInfo{ public static void zhaohui() {
Scanner s = new Scanner(System.in);
System.out.println("請輸入你要找回的用戶名:");
String zname = s.nextLine(); for(int i=0;i<2;i++) { if(user[i].equals(zname)) {
Scanner ss = new Scanner(System.in);
System.out.println("恭喜你!成功找回密碼,請輸入:"+"'張哥最帥'"+" 查看密碼");
String zgzs = ss.nextLine();
if("張哥最帥".equals(zgzs)) {
System.out.println(passwd[i]);
}else {
System.out.println("請輸正確!");
}
}else if(user[i]!=zname){
System.out.println("用戶名不存在!"); return;
}
break;
}
}
}//修改密碼 class XiuGai extends UserInfo{ public static void xiugai() {
Scanner s =new Scanner(System.in);
System.out.println("請輸入您要修改的密碼:");
String xpasswd = s.nextLine(); for(int i=0;i<2;i++) {
passwd[i] = xpasswd; if(xpasswd.equals(passwd[i])) {
System.out.println("恭喜你,修改成功!"); break;
}else {
System.out.println("修改密碼失敗"); break;
}
}
}
}//查詢用戶class ChaXun extends UserInfo{
public static void select() { for(int i=0;i<2;i++) {
System.out.println("當前用戶:"+user[i] +"\n"+ "當前密碼:"+passwd[i] );
i++; break;
}
}
}//注冊class ZhuCe extends UserInfo{
public static void regist() {
Scanner ss = new Scanner(System.in);
System.out.println("請輸入用戶名:");
String suser = ss.nextLine();
System.out.println("請輸入密碼:");
String spasswd = ss.nextLine();
for(int i=0;i<user.length;i++) {
user[i] = suser;
passwd[i] = spasswd;
System.out.println("注冊成功!"); break;
}
}
}//登錄class Loginc extends UserInfo{
public static void login() { int flag = 1;
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入用戶名:");
String users = scanner.nextLine();
System.out.println("請輸入密碼:");
String passwds = scanner.nextLine();
for(int i=0;i<UserInfo.user.length;i++) { if(user[i].equals(users) && passwd[i].equals(passwds)) {
System.out.println("登陸成功!"); break;
}
System.out.println("登陸失敗!"); break;
}
}
}//主界面class ZhuJieMian{ public static void Start() {
Loginc Loginc = new Loginc();
ZhuCe ZhuCe = new ZhuCe();
ChaXun ChaXun = new ChaXun();
XiuGai XiuGai = new XiuGai();
ZhaoHui ZhaoHui = new ZhaoHui();
Scanner s = new Scanner(System.in); while(true) {
System.out.println("|"+ "\t"+ "\t"+ "\t"+ "\t"+ "\t"+ "\t"+ "\t"+"\t"+"|");
System.out.println("|"+"\t" + "測試用戶名:test 測試密碼:123456" + "\t"+ "\t"+ "\t"+ "\t"+"|");
System.out.println("|" + "\t"+ "請輸入[1-5]進行操作 1.登錄|2.注冊|3.查詢當前用戶|4.修改密碼|5.找回密碼 " + "\t"+"|");
System.out.print("請輸入:"); int temp = s.nextInt();
switch(temp) { case 1:Loginc.login(); break; case 2:ZhuCe.regist();; break; case 3:ChaXun.select();; break; case 4:XiuGai.xiugai();; break; case 5:ZhaoHui.zhaohui();; break; default:System.out.println("錯誤!請重寫輸入正確的數字進行操作!");
}
}
}
}public class LoginTest { public static void main(String[] args) {
ZhuJieMian zjm = new ZhuJieMian();
zjm.Start();
}
}
F. 鐢╦ava綾繪ā鎷熸父鎴忕殑璐﹀彿鐨勭櫥闄嗭紝娉ㄥ唽錛岄鍑恆 1.娌℃湁璐﹀彿錛岄渶瑕佹敞鍐屻 2.娉ㄥ唽瀹屾垚鍚庢柟鍙鐧
/**
闇奼傦細濂栧㈠瘜緲佹父鎴忋
鑿滃崟涓猴細
1.娉ㄥ唽鐢ㄦ埛淇℃伅銆傚~鍐欑敤鎴峰悕錛屽瘑鐮侊紝鐢佃剳闅忔満杈撳嚭涓涓4浣
鏁板瓧浣滀負浼氬憳鍗″彿銆傛彁紺虹敤鎴峰悕錛屽瘑鐮侊紝鍙婁細鍛樺崱鍙楓傚彲閫夋嫨絎浜屾ヤ簡
2.鐧婚檰銆傛牎楠屾槸鍚﹀凡娉ㄥ唽錛堟槸鍚﹀瓨鍦ㄤ細鍛樺崱鍙鳳級銆傚傛灉涓虹┖錛
鎻愮ず鐢ㄦ埛涓嶅瓨鍦ㄣ
濡傛灉鐢ㄦ埛鍚嶉氳繃錛屽瘑鐮侀敊璇錛屾彁紺虹敤鎴峰悕鍜屽瘑鐮佷笉鍖歸厤銆傚傛灉
楠岃瘉鎴愬姛緇撴潫銆傚傛灉杈撳叆鐨勭敤鎴峰悕瀵嗙爜鍧囧尮閰嶏紝緇欏嚭鐧婚檰鎴愬姛鎻愮ず銆
鍙閫夋嫨絎涓夋ヤ簡銆
3.鍏堝垽鏂鐢ㄦ埛鏄鍚﹀凡鐧婚檰錛堟槸鍚﹀瓨鍦ㄤ細鍛樺崱錛
璇㈤棶鏄鍚﹁繘琛屾娊濂栵紝閫塝錛岀數鑴戦殢鏈鴻緭鍑5涓4浣
鏁板瓧銆傚傛灉鍏朵腑鏈変竴涓鏁板瓧涓庣敤鎴峰崱鍙風浉鍚岋紝鍒欎負涓濂栵紝濂栧姳
浜烘皯甯佷負鍗″彿鏁板瓧鐨勯噾棰濄傚惁鍒欙紝璋㈣阿鍙備笌銆傜▼搴忛鍑恆
*/
importjava.util.Scanner;
importjava.util.Random;
publicclassRichPerson{
publicstaticvoidmain(String[]args){
//杈撳嚭鑿滃崟銆
for(inta=0;a<6;a++){
System.out.print("*");
}
System.out.print("嬈㈣繋榪涘叆濂栧㈠瘜緲佺郴緇");
for(intb=0;b<6;b++){
System.out.print("*");
}
System.out.print(" 1.娉ㄥ唽 "+" 2.鐧誨綍 "+" 3.鎶藉朶n");
for(intc=0;c<31;c++){
System.out.print("*");
}
//杈撳嚭鑿滃崟緇撴潫銆
intcaidan=0;//鎿嶄綔鑿滃崟
Stringname=null;//浼氬憳娉ㄥ唽鍚嶅瓧
Stringmyname=null;//浼氬憳鐧誨綍鍚嶅瓧
Stringpassword=null;//浼氬憳娉ㄥ唽瀵嗙爜
Stringmypassword=null;//浼氬憳鐧誨綍瀵嗙爜
intnumber=0;//浼氬憳鍗″彿
//intluckynumber=0;//騫歌繍鍙風爜
Stringstr=null;//璇㈤棶鏄鍚﹂噸璇
//杈撳叆鏁板瓧閫夋嫨鑿滃崟銆
Scannerinput=newScanner(System.in);
Randomoutput=newRandom();
do{
System.out.println(" 璇烽夋嫨鑿滃崟錛");
caidan=input.nextInt();
switch(caidan){
case1:
System.out.println("嬈㈣繋娉ㄥ唽瀵岀縼錛岃蜂緷嬈¤緭鍏ョ敤鎴峰悕鍜屽瘑鐮");
System.out.println("鐢ㄦ埛鍚嶏細");
name=input.next();
System.out.println("瀵嗙爜錛");
password=input.next();
//闅忔満緇欏嚭4浣嶆暟鐨勪細鍛樺崱鍙
number=output.nextInt(8999)+1000;
System.out.println("娉ㄥ唽鎴愬姛錛佹偍鐨勭敤鎴峰悕錛"+name+"錛屽瘑鐮侊細"+password
+"錛屼細鍛樺崱鍙鳳細"+number+"銆傝風墷璁幫紒");
break;
case2:
//鍏堝垽鏂鐢ㄦ埛鏄鍚﹀凡娉ㄥ唽銆
if(name==null){
System.out.println("鎮ㄥソ錛岃峰厛娉ㄥ唽錛");
break;
}else{
System.out.println("嬈㈣繋鐧誨綍瀵岀縼錛岃蜂緷嬈¤緭鍏ョ敤鎴峰悕鍜屽瘑鐮侊紒");
//鐢ㄦ埛鍚嶅拰瀵嗙爜鍖歸厤鏍¢獙嬈℃暟璁劇疆涓3嬈°
for(intx=0;x<3;x++){
System.out.println("鐢ㄦ埛鍚嶏細");
myname=input.next();
if(!myname.equals(name)){
System.out.println("鐢ㄦ埛"+myname+"涓嶅瓨鍦錛佽烽噸璇曪紝鍓╀綑嬈℃暟"
+(2-x)+"嬈★紒");
}else{
System.out.println("瀵嗙爜錛");
}
mypassword=input.next();
if(!mypassword.equals(password)){
System.out.println(myname+"瀵嗙爜閿欒錛"+"璇烽噸璇曪紝鍓╀綑嬈℃暟"
+(2-x)+"嬈★紒");
}else{
System.out.println("鐧婚檰鎴愬姛錛");
break;
}
}
}
break;
case3:
//鍒ゆ柇鐢ㄦ埛鏄鍚﹀凡娉ㄥ唽錛屾槸鍚﹀凡鐧誨綍銆
if(mypassword==null){
System.out.println("鎮ㄥソ錛岃峰厛鐧誨綍錛");
break;
}elseif(!mypassword.equals(password)){
System.out.println("鎮ㄥソ錛岃峰厛閲嶈瘯鐧誨綍錛");
break;
}elseif(mypassword.equals(password)){
System.out.println("嬈㈣繋榪涘叆瀵岀縼錛岃烽棶鏄鍚﹁繘琛屽垢榪愭娊濂(y/n)錛");
Stringchoujiang=input.next();
if(choujiang.equals("y")){
int[]luckynumber=newint[5];
for(intm=0;m<5;m++){
luckynumber[m]=output.nextInt(8999)+1000;
}
for(intm=0;m<5;m++){
System.out.print(luckynumber[m]+" ");
}
System.out.println(" "+number);
for(intm=0;m<5;m++){
if(number==luckynumber[m]){
System.out.println(" 涓濂栵紒閲戦濅負錛"+luckynumber);
break;
}else{
System.out.println(" 鏈ㄦ湁涓濂栵紒");
break;
}
}
}else{
System.out.println("紼嬪簭緇撴潫錛");
break;
}
}
break;
default:
System.out.println("娌℃湁瀵瑰簲鐨勮彍鍗曢夐」錛佽烽噸璇");
break;
}
System.out.println("鏄鍚︾戶緇(y/n)?");
str=input.next();
}while(str.equals("y"));
System.out.println("紼嬪簭緇撴潫錛");
}
}
緇欎釜浠g爜 鑷宸辯帺鐜
G. 求用java實現簡單的登錄和注冊功能,主要是為了了解怎樣對資料庫進行增刪改查。和各種請求處理,
//User 用戶的基本信息,也是USERINFO表中的3個列
package
登陸判斷;
public class User {
private String name;
private String loginname;
private String
loginpsw;
public String getName() {
return name;
}
public
void setName(String name) {
this.name = name;
}
public String
getLoginname() {
return loginname;
}
public void
setLoginname(String loginname) {
this.loginname =
loginname;
}
public String getLoginpsw() {
return
loginpsw;
}
public void setLoginpsw(String loginpsw)
{
this.loginpsw = loginpsw;
}
}
//用於注冊和登陸的小小代碼塊
package
登陸判斷;
import java.util.Scanner;
public class In {
public static User getUser(){
User u = new User();
Scanner sc =
new
Scanner(System.in);
System.out.println("請輸入登陸名");
u.setLoginname(sc.nextLine());
System.out.println("請輸入密碼");
u.setLoginpsw(sc.nextLine());
return
u;
}
public static User registerUser(){
User u = new
User();
Scanner sc = new
Scanner(System.in);
System.out.println("請注冊用戶名名");
u.setName(sc.nextLine());
System.out.println("請注冊登陸名");
u.setLoginname(sc.nextLine());
System.out.println("請注冊密碼");
u.setLoginpsw(sc.nextLine());
return
u;
}
}
//登陸時的檢查,判斷登陸名和密碼是否正確,正確則返回 用戶名
package 登陸判斷;
import java.sql.Connection;
import java.sql.DriverManager;
import
java.sql.PreparedStatement;
import java.sql.ResultSet;
import
java.sql.SQLException;
public class Check {
public static void checkUser(User u)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
String
url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user =
"scott";
String password = "tiger";
Connection conn =
null;
ResultSet rs = null;
PreparedStatement pst =
null;
String sql = "Select name from USERINFO where loginname = ? and
loginpsw = ? ";
try {
conn = DriverManager.getConnection(url, user,
password);
pst = conn.prepareStatement(sql);
pst.setString(1,
u.getLoginname());
pst.setString(2,u.getLoginpsw());
rs =
pst.executeQuery();
while(rs.next()){
u.setName(rs.getString(1));
}
}
catch (SQLException e) {
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
conn.close();
} catch
(SQLException e)
{
e.printStackTrace();
}
}
if(u.getName() == null ||
u.getName().isEmpty()){
System.out.println("登陸失敗");
}else{
System.out.println("歡迎"+u.getName()+"登陸");
}
}
}
//注冊界面,要判斷 登陸名是否有重復,有重復則注冊失敗。有點小疑問,已用注釋標出來
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 Register {
public static void registUser(User u){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch
(ClassNotFoundException e) {
e.printStackTrace();
}
String
url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user =
"scott";
String password = "tiger";
Connection conn =
null;
String sql = "Insert Into USERINFO
values(?,?,?)";
PreparedStatement pst =
null;
//判斷登陸名是否已經存在
Statement s = null;
ResultSet rs =
null;
String sql_ck = "Select LOGINNAME FROM USERINFO";
try
{
conn = DriverManager.getConnection(url, user, password);
pst =
conn.prepareStatement(sql);
pst.setString(1,
u.getName());
pst.setString(2, u.getLoginname());
pst.setString(3,
u.getLoginpsw());
//判斷登陸名是否已經存在
s =
conn.createStatement();
rs =
s.executeQuery(sql_ck);
while(rs.next()){//為什麼用 rs.getString(2)會提示
無效的索引呢
if( rs.getString("LOGINNAME").equals(u.getLoginname())
){
System.out.println("登陸名已經存在,注冊失敗");
break;
}else{
pst.executeUpdate();
System.out.println("注冊成功");
break;
}
}
}
catch (SQLException e) {
e.printStackTrace();
}finally{
try
{
rs.close();
pst.close();
s.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
//main界面//
//資料庫中表的結構式這樣
//name(用戶名,用於顯示),
//loginname(登錄名,即登陸時輸入的ID)
//loginpsw(登陸時輸入的密碼)
package
登陸判斷;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int chos = 0;
Scanner sc = new
Scanner(System.in);
System.out.println("\t\t登陸請按1\t\t注冊請按2");
chos =
sc.nextInt();
switch(chos){
case 1:
Check.checkUser(In.getUser());break;
case 2:
Register.registUser(In.registerUser());break;
default:
System.out.println("請輸入正確的選擇");break;
}
}
}