java登錄與注冊-九游会j9娱乐平台
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(nhourreturntrue;
}
//23鐐歸渶瑕佸崟鐙鍒ゆ柇錛岃秴榪23鐐30灝卞簲璇ヤ紤鎮
if((nhour>=m_nusecomputermaxhour)&&(nminute>=m_nminutes)){
returntrue;
}
//3.闈炰紤鎮鏃墮棿
returnfalse;
}
/**
*鍒ゆ柇鏄鍚︽湁浜轟慨鏀逛簡緋葷粺鏃墮棿錛屽傛灉鏈変漢淇鏀逛簡緋葷粺鏃墮棿榪斿洖true錛
*鍚﹀垯榪斿洖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[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
}
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;
}
}
}