package cc.icoc.javaxu.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class MySQLOprea { /** * 增加記錄 INSERT INTO 表名(字段名,字段名) VALUES (值,值); * 刪除記錄 DELETE FROM 表名 WHERE 條件() * 修改記錄 UPDATE 表名 SET 字段=值,字段=值 WHERE 條件 * 查詢(xún)記錄 SELECT 字段,字段 FROM 表名 WHERE 條件 */ ResultSet rs = null; Connection conn = null; Statement statement = null; //鏈接 public Connection connSQL() { String DRIVER = "com.mysql.jdbc.Driver";// 數(shù)據(jù)庫(kù)驅(qū)動(dòng) String URL = "jdbc:mysql://localhost:3306/mydata?useUnicode=truecharacterEncoding=gb2312";// String DBNAME = "root";// 用戶(hù)名 String DBPASS = "341341";// 密碼 try { Class.forName(DRIVER).newInstance();// 注冊(cè)驅(qū)動(dòng) conn = DriverManager.getConnection(URL, DBNAME, DBPASS); statement = conn.createStatement(); } catch (Exception e) {} return conn; } //增 /** * 插入新記錄的操作 * @param table 表名 * @param userName 插入的用戶(hù)名 * @param passWord 插入的用戶(hù)密碼 * @return true代表插入成功,false代表插入失敗 */ public String insert(String table, String userName, String passWord) { connSQL(); String s = "注冊(cè)成功"; try { String insert = "insert into "+table+"(userName,passWord) values ("+"'"+userName+"'"+","+"'"+passWord+"'"+")"; statement.executeUpdate(insert); closeDB(); } catch (Exception e) { // TODO: handle exception s = "注冊(cè)失敗"+e.toString(); } return s; } //刪 public void delete(String table, String whereValue) throws SQLException { String delete = "Delete from "+table+" where userName = "+whereValue; statement.executeUpdate(delete); } //改 public void update(String table, String whereValue , String newValue) throws SQLException { String update = "Update "+table+" set passWord ="+newValue+" where userName ="+whereValue; statement.executeUpdate(update); } //查 public String query(String table , String whereValue1 ,String whereValue2, String whatCol1, String whatCol2) throws SQLException { connSQL(); String query = null;// ResultSet set= null; try { query = "select "+whatCol1+","+whatCol2+" from "+table +" where "+whatCol1+"="+'"'+whereValue1+'"'+" and "+whatCol2+"="+'"'+whereValue2+'"'; rs = statement.executeQuery(query); closeDB(); } catch (Exception e) { // TODO: handle exception return "false exception:"+e.toString(); } if(rs.next()) { return "true:"; } return "false:"; } private void closeDB() { // TODO Auto-generated method stub try { if(rs != null) { rs.close(); } if(statement != null) { statement.close(); } if(conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception System.out.println("數(shù)據(jù)庫(kù)關(guān)閉時(shí)出現(xiàn)異常"); } }}

創(chuàng)新互聯(lián)建站的客戶(hù)來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜希瑥膭?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。專(zhuān)業(yè)領(lǐng)域包括成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、電商網(wǎng)站開(kāi)發(fā)、微信營(yíng)銷(xiāo)、系統(tǒng)平臺(tái)開(kāi)發(fā)。
這篇文章主要介紹了java通過(guò)JFrame做一個(gè)登錄系統(tǒng)的界面完整代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
在java的JFrame內(nèi)通過(guò)創(chuàng)建匿名對(duì)象的方式做登錄界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登錄");
JButton bg=new JButton("關(guān)閉");
//構(gòu)造無(wú)參構(gòu)造器把主要的方法放在構(gòu)造器里,然后在main方法里面調(diào)
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用戶(hù)名"));
c.add(txtname);
c.add(new JLabel("密碼"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此處是匿名內(nèi)部類(lèi)
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此處是匿名內(nèi)部類(lèi)
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
你這個(gè)不用數(shù)據(jù)庫(kù)真的是有點(diǎn)難搞
我寫(xiě)了個(gè)用集合存儲(chǔ)的,你看看,能否幫上你
java.util.ListString?list?=?new?ArrayListString();
list.add("qq=123");//存儲(chǔ)的時(shí)候用(用戶(hù)名=密碼)的形式
list.add("ww=456");
String?username?=?"qq";
String?password?=?"123";
for?(int?i?=?0;?i??list.size();?i++)?{
String?num?=?username?+"="+password;
if(num.equals(list.get(i))){
System.out.println("登錄成功");
break;
}
}
Java寫(xiě)提示注冊(cè)成功的方法如下:
1、首先用戶(hù)注冊(cè)完成后,返回一個(gè)boolean值的變量;
2、利用Servlet類(lèi)判斷這個(gè)變量,如果為true,跳轉(zhuǎn)到提示界面,提示用戶(hù)注冊(cè)成功,如果為false,跳轉(zhuǎn)到提示界面,提示用戶(hù)注冊(cè)失敗;
3、具體代碼如下所示:
public?class?DemoServlet?extends?HttpServlet?{
public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
String?username?=?request.getParameter("username");
String?usepwd=?request.getParameter("usepwd");
boolean?flag?=?Dao.register(username,usepwd);//注冊(cè)方法
if(flag){
//提示注冊(cè)成功
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}else{
//提示注冊(cè)失敗
request.getRequestDispatcher("/success.jsp").forward(request,?response);
}
}
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
doGet(request,?response);
}
}
4、至此,就完成了提示注冊(cè)成功的功能。
首先,我們得明白用戶(hù)登錄使用什么登陸的,即用戶(hù)在線的原理。這只是將用戶(hù)的對(duì)象存放在了session中而已,然后再frame中進(jìn)行調(diào)用,其他特定頁(yè)面也進(jìn)行直接引用就行。那么實(shí)現(xiàn)“擠下來(lái)”的功能就是讓新生成的session有效,讓原來(lái)存放用戶(hù)的session失效就行。到此,大體思路已經(jīng)有了。那怎么實(shí)現(xiàn)呢?
想要知道怎么實(shí)現(xiàn),就必須要明白session存放用戶(hù)對(duì)象的過(guò)程了。在用戶(hù)登錄之后,我們可以得到用戶(hù)的對(duì)象user,而存放到session中需要執(zhí)行session.setAttribute(key,value); 我們將用戶(hù)的userId或是其他的唯一標(biāo)識(shí)存為key,將用戶(hù)對(duì)象存為值。這樣就能隨時(shí)隨地調(diào)用唯一用戶(hù)了。user存放的問(wèn)題解決了,那相同 登錄 時(shí)session廢除的問(wèn)題呢?
?
這個(gè)其實(shí)也不難,我們可以更具session的特性一樣,用map進(jìn)行存貯,將用戶(hù)的標(biāo)識(shí)存為key,而將其所對(duì)應(yīng)的session存為value,那么當(dāng)重復(fù)用戶(hù)登錄時(shí),只需要取出對(duì)應(yīng)的session,將其invalidate就行了。
至此,實(shí)現(xiàn)思路已經(jīng)明了,聒噪了這么久,大家都急不可耐地想看代碼了吧?以下是代碼:
前置準(zhǔn)備,jsp界面
界面很簡(jiǎn)單,只是一個(gè)簡(jiǎn)單的登錄界面
form action ="%=request.getContextPath()%/UserWXPServlet" method = "post"
用戶(hù)名?input type = "text" name = "username"/br/
密碼?input type = "text" name = "password"/br/
input type = "submit" value ="提交"/
/form
成功后跳轉(zhuǎn)頁(yè)面
歡迎:${sessionScope.user.username}登陸!br/
我這沒(méi)有寫(xiě)失敗頁(yè)面,大家可以自己寫(xiě),失敗頁(yè)面也沒(méi)什么好說(shuō)的了
entity和登錄的實(shí)現(xiàn)
user的javabean
private String username;
private String password;
public User() {
}
public User(String user, String password) {
super();
this.username = user;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
登錄user的service實(shí)現(xiàn)方法,這里就不寫(xiě)dao和接口了,一切以簡(jiǎn)單為
public boolean dologin(User user){
Properties pro = new Properties();
InputStream is = UserWXPServlet.class.getClassLoader().getResourceAsStream("user_wxp.properties");
String password = null;
System.out.println(is+"---------"+pro);
if(user==null){
return false;
}
try {
pro.load(is);
password = pro.getProperty(user.getUsername());
if(user.getPassword()!=nulluser.getPassword().equals(password)){
System.out.println("登陸成功");
return true;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
登錄成功返回true,失敗則返回false。
給你提供個(gè)思路吧
1、注冊(cè)界面把注冊(cè)的人的用戶(hù)名和密碼存儲(chǔ)到本地的一個(gè)txt文件中
2、登錄時(shí)比較登錄輸入的用戶(hù)名和密碼和txt文件中的是否一致,如果一致就允許登錄,不一致提示異常
當(dāng)前題目:java在線注冊(cè)代碼 java編程網(wǎng)站
文章路徑:http://chinadenli.net/article42/hhgcec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、品牌網(wǎng)站設(shè)計(jì)、微信小程序、響應(yīng)式網(wǎng)站、全網(wǎng)營(yíng)銷(xiāo)推廣、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)