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;
}
}
}