簡單的登錄系統(java+JFrame+Mysql)

連接數據庫

package 注冊信息;    
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;  public class conn_db{  Connection con;  String url = null;  Statement stmt;  public void connection() throws ClassNotFoundException{  //url = "jdbc:mysql://localhost:3306/wang?" +"user=root&password=wsq & useUnicode=true & characterEnunicode=UTF8";  //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08?useUnicode=true&characterEncoding=utf-8&useSSL=true","root", "wsq");/*try{  Class.forName("com.mysql.jdbc.Driver");  con = DriverManager.getConnection(url);  System.out.println("連接成功");  stmt = con.createStatement();  }  catch(SQLException e){  e.printStackTrace();  }  */url = "jdbc:mysql://localhost:3306/wang?user=root&password=wsq&useUnicode=true&characterEncoding=UTF8";  
//這里的數據庫密碼每個人的都不同,需要更改try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection(url);System.out.println("連接成功");  stmt = con.createStatement();/*Statement s =  con.createStatement();System.out.println(con.isClosed());*/}catch (Exception e) {e.printStackTrace();}}  public static void main(String[] args) throws ClassNotFoundException {  conn_db conn = new conn_db();  conn.connection();  }  
}  

注冊信息,在數據庫里面進行檢查

package 注冊信息;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;import javax.swing.*;public class login_db extends conn_db implements ActionListener {JTextField accT,nameT;JButton okB,registB;register re;ResultSet rs;public void setaccountT(JTextField a){accT = a;}public void setnameT(JTextField n){nameT = n;}public void setButton(JButton b1,JButton b2){okB = b1;registB = b2;}public void actionPerformed(ActionEvent e){if(e.getSource() == okB){if(accT.getText().equals(""))			//判斷用戶輸入是否為空;JOptionPane.showMessageDialog(null, "請填寫賬號!");else if(nameT.getText().equals(""))JOptionPane.showMessageDialog(null, "請輸入密碼");else{String accountT = accT.getText();String namesT = nameT.getText();try {connection(); 		//加載conn_db類,連接數據庫;boolean com = compareWithSql(accountT,namesT);if(com)JOptionPane.showMessageDialog(null, "登錄成功");else{JOptionPane.showMessageDialog(null, "賬號或密碼不正確,請重新輸入");accT.setText("");nameT.setText("");}} catch (Exception e1) {e1.printStackTrace();}}}else if(e.getSource() == registB){new JFrame().dispose();re = new register();}}//賬號輸入檢查boolean compareWithSql(String accountT,String namesT) throws Exception{String sql;		Connection con = super.con;Statement stmt = con.createStatement();sql = "select * from my";
//		System.out.println(sql);rs = stmt.executeQuery(sql);while(rs.next()){				//用戶輸入的信息和數據庫中的信息做比較,判斷輸入是否正確;String acc = rs.getString(1);String names = rs.getString(2);if(acc.equals(accountT) && names.equals(namesT)){//break;return true;}
//			System.out.println(acc + "   " + names);
//			System.out.println(accountT + "   " + namesT);}
//		System.out.println("hahahaha");return false;}}

JFrame窗口登錄頁面

package 注冊信息;import java.awt.FlowLayout;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.*;public class login extends JFrame{//JLabel accountL,nameL;JTextField accountT,nameT;JButton okB,registB;Box baseB1,baseB2,box1,box2,box3;		//此登錄頁面采用Box布局方式;login_db log;login(){init();}void init(){log = new login_db();accountT = new JTextField(10);nameT = new JTextField(20);okB = new JButton("登錄");registB = new JButton("注冊");box1 = Box.createVerticalBox();box1.add(new JLabel("賬號:"));box1.add(Box.createVerticalStrut(8));box1.add(new JLabel("密碼"));box2 = Box.createVerticalBox();box2.add(accountT);box2.add(Box.createVerticalStrut(8));box2.add(nameT);box3 = Box.createHorizontalBox();box3.add(okB);box3.add(Box.createHorizontalStrut(20));box3.add(registB);baseB1 = Box.createHorizontalBox();baseB1.add(box1);baseB1.add(Box.createHorizontalStrut(8));baseB1.add(box2);baseB2 = Box.createVerticalBox();baseB2.add(baseB1);baseB2.add(Box.createVerticalStrut(10));baseB2.add(box3);okB.addActionListener(log);registB.addActionListener(log);log.setaccountT(accountT);log.setnameT(nameT);log.setButton(okB,registB);add(baseB2);setLayout(new FlowLayout());setBounds(200,150,400,300);setVisible(true);setTitle("用戶登錄界面");setDefaultCloseOperation(DISPOSE_ON_CLOSE);}public static void main(String[] args) {login lo = new login();}
}

創建并檢查數據庫表

package 注冊信息;  import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.sql.Connection;  
import java.sql.ResultSet;  
import java.sql.Statement;  import javax.swing.*;  public class register_db extends conn_db implements ActionListener{  JTextField textacc,textname;  JButton okButton,resetButton;  Statement stmt;  ResultSet rs;  
//  double acc;  
//  String name;  
//  Connection con = null;  public void setaccountField(JTextField a){  textacc = a;  }  public void setnameField(JTextField n){  textname = n;  }  public void setokButton(JButton b1){  okButton = b1;  }  public void setresetButton(JButton b2){  resetButton = b2;  }  public void actionPerformed(ActionEvent e){  if(e.getSource() == okButton){  if(textacc.getText().equals(""))            //判斷用戶輸入是否為空;  JOptionPane.showMessageDialog(null, "請輸入賬號","警告對話框",JOptionPane.WARNING_MESSAGE);  else if(textname.getText().equals(""))  JOptionPane.showMessageDialog(null,"請輸入姓名","警告對話框",JOptionPane.WARNING_MESSAGE);  else{  String acc = textacc.getText();  String name = textname.getText();  try {  connection();  writeInSql(acc,name);  } catch (Exception e1) {  System.out.println("插入失敗");  e1.printStackTrace();  }  }  }  else if(e.getSource() == resetButton){  textacc.setText("");  textname.setText("");  }  }  void writeInSql(String acc,String name) throws Exception{  String sql;  Connection con = super.con;  Statement stmt = con.createStatement();  //創建并檢查數據庫表,若沒有"my"表,則創建表并執行插入操作,若表已存在,則直接執行操作,插入數據  sql = "create table if not exists my(account varchar(10),name varchar(20))";  stmt.executeUpdate(sql);  
//      System.out.println(sql);        //查錯;  System.out.println("創建表成功");      //插入存文本框中獲取的數據;  sql = "insert into my(account,name) values('"+acc+"','"+name+"')";  int rw = stmt.executeUpdate(sql);  
//      System.out.println(sql);  if(rw <= 0){             //判斷數據是否插入成功  JOptionPane.showMessageDialog(null,"注冊失敗");  }  else{  JOptionPane.showMessageDialog(null, "注冊成功");  }  }  
}

用戶頁面注冊

package 注冊信息;import javax.swing.JFrame;import java.awt.FlowLayout;
import javax.swing.*;public class register extends JFrame{JLabel accountLabel,nameLabel;JButton okButton,resetButton;JTextField accountText,nameText;Box baseBox1,baseBox2, box1,box2,box3;		 //此注冊頁面采用Box布局方式;//JPanel pane1,pane2;register_db regist;register(){init();}void init(){setLayout(new FlowLayout());accountLabel = new JLabel("賬號");nameLabel= new JLabel("姓名");accountText = new JTextField(10);nameText = new JTextField(20);okButton = new JButton("確定");resetButton = new JButton("重置");regist = new register_db();//		lab = new JLabel("用戶注冊頁面");box1 = Box.createVerticalBox();box1.add(accountLabel);box1.add(Box.createVerticalStrut(8));box1.add(nameLabel);box2 = Box.createVerticalBox();box2.add(accountText);box2.add(Box.createVerticalStrut(8));box2.add(nameText);box3 = Box.createHorizontalBox();box3.add(okButton);box3.add(Box.createHorizontalStrut(15));box3.add(resetButton);baseBox1 = Box.createHorizontalBox();baseBox1.add(box1);baseBox1.add(Box.createHorizontalStrut(8));baseBox1.add(box2);baseBox2 = Box.createVerticalBox();baseBox2.add(baseBox1);baseBox2.add(Box.createVerticalStrut(10));baseBox2.add(box3);add(baseBox2);okButton.addActionListener(regist);resetButton.addActionListener(regist);regist.setaccountField(accountText);regist.setnameField(nameText);regist.setokButton(okButton);regist.setresetButton(resetButton);setBounds(200,200,400,300);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);setTitle("用戶注冊界面");}//	void registAction(){
//		
//	}//	public static void main(String[] args) {
//		register re = new register();
//	}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379768.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379768.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379768.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

冬季止咳化痰的飲食偏方集錦

1、蘿卜蔥白風寒咳嗽 蘿卜1個,蔥白6根,生姜15克.用水三碗先將蘿卜煮熟,再放蔥白,姜,煮剩一碗湯.連渣一次服.宣肺解表,化痰止咳.治風寒咳嗽,痰多泡沫,伴畏寒,身倦酸痛等. 2、紅糖姜棗湯治傷風咳嗽 紅糖30克,鮮姜15克,紅棗30克. 以水三碗煎至過半.頓服,服后出微汗即愈. 驅風散寒.…

c語言中數組越界怎么辦_如果我們使用C語言數組中的越界索引怎么辦?

c語言中數組越界怎么辦Let’s understand first, what is index out of bounds? 首先讓我們了解一下 &#xff0c; 什么是索引超出范圍&#xff1f; Let suppose you have an array with 5 elements then the array indexing will be from 0 to 4 i.e. we can access element…

FreeRTOS任務基礎知識

任務特性 在RTOS中&#xff0c;一個實時應用可以作為一個獨立的任務&#xff0c;支持搶占&#xff0c;支持優先級&#xff0c;每個任務都有自己的堆棧&#xff0c;當任務切換時將上下文環境保存在堆棧中&#xff0c;再次調用任務時&#xff0c;取出上下文信息&#xff0c;繼續…

測試Rockey 4 Smart加密鎖的C語言代碼

測試Rockey 4 Smart加密鎖的C語言代碼 // win32Console_dog_test.cpp : Defines the entry point for the console application. /// // //測試Rockey 4 Smart加密鎖的C語言代碼 // /// #include "stdafx.h" #include <conio.h> #include "time.h" #…

C——任意一個偶數分解兩個素數

題目&#xff1a;一個偶數總能表示為兩個素數之和 以上實例運行輸出結果為&#xff1a; 請輸入一個偶數: 4 偶數4可以分解成1和3兩個素數的和 #include <stdio.h> #include <stdlib.h> int Isprimer(int n); int main() {int n,i;do{printf("請輸入一個偶數&…

c#委托調用另一窗口函數_在C#中使用委托調用成員函數

c#委托調用另一窗口函數Prerequisite: Delegates in C# 先決條件&#xff1a; C&#xff03;中的代表 We can also call a member function of a class using delegates. It is similar to static function calls, here we have to pass member function using an object on t…

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互 示例程序下載地址&#xff1a;http://download.csdn.net/source/999273&#xff08;源碼在jar內&#xff09; AVG&#xff0c;即Adventure Game&#xff0c;可以直譯為[冒險游戲]。但是通常情況下我們說AVG是指[文字冒險…

FreeRTOS任務創建和刪除

任務創建和刪除的API函數 xTaskCreate()&#xff1a;使用動態方法創建一個任務xTaskCreateStatic()&#xff1a;使用靜態方法創建一個任務xTaskCreateRestricated()&#xff1a;創建一個使用MPU進行限制的任務&#xff0c;相關內存使用動態內存分配vTaskDelete()&#xff1a;刪…

Delphi 調試

調試&#xff1a;F9執行F8逐過程單步調試F7逐語句單步調試轉載于:https://www.cnblogs.com/JackShao/archive/2012/04/30/2476931.html

1.創建單項鏈表

# include <stdio.h> # include <malloc.h> # include <stdlib.h>typedef struct Node{int data;//數據域struct Node *pNext;//指針域}NODE, *PNODE; //NODE等價于struct Node //PNOD等價于struct Node * //函數聲明PNODE create_list(void); void traverse…

python 日本就業_日本的繪圖標志 Python中的圖像處理

python 日本就業Read basics of the drawing/image processing in python: Drawing flag of Thailand 閱讀python中繪圖/圖像處理的基礎知識&#xff1a; 泰國的繪圖標志 The national flag of Japan is a rectangular white banner bearing a crimson-red disc at its center…

[windows phone 7 ]查看已安裝程序GUID

首先介紹下wp7RootToolsSDK,這個功能相當強大&#xff0c;適合研究wp7高級功能。 它支持File&#xff0c;Register操作&#xff0c;比之前的COM調用要簡單&#xff0c;方便。 功能:查看已安裝程序的guid 開發心得: 用的是mozart,rom多&#xff0c;刷機吧&#xff0c;最麻煩的是…

FreeRTOS任務掛起和恢復

任務掛起&#xff1a;暫停某個任務的執行 任務恢復&#xff1a;讓暫停的任務繼續執行 通過任務掛起和恢復&#xff0c;可以達到讓任務停止一段時間后重新運行。 相關API函數&#xff1a; vTaskSuspend void vTaskSuspend( TaskHandle_t xTaskToSuspend );xTaskToSuspend &am…

向oracle存儲過程中傳參值出現亂碼

在頁面中加入<meta http-equiv"Content-Type" content"text ml;charsetUTF-8"/>就可以解決這一問題 適用情況&#xff1a; 1.中文 2.特殊符號 轉載于:https://www.cnblogs.com/GoalRyan/archive/2009/02/16/1391348.html

Scala程序將多行字符串轉換為數組

Scala | 多行字符串到數組 (Scala | Multiline strings to an array) Scala programming language is employed in working with data logs and their manipulation. Data logs are entered into the code as a single string which might contain multiple lines of code and …

SQL 異常處理 Begin try end try begin catch end catch--轉

SQL 異常處理 Begin try end try begin catch end catch 總結了一下錯誤捕捉方法:try catch ,error, raiserror 這是在數據庫轉換的時候用的的異常處理, Begin TryInsert into SDT.dbo.DYEmpLostTM(LogDate,ProdGroup,ShiftCode,EmployeeNo,MONo,OpNo,OTFlag,LostTypeID,OffStd…

FreeRTOS中斷配置與臨界段

Cortex-M中斷 中斷是指計算機運行過程中&#xff0c;出現某些意外情況需主機干預時&#xff0c;機器能自動停止正在運行的程序并轉入處理新情況的程序&#xff08;中斷服務程序&#xff09;&#xff0c;處理完畢后又返回原被暫停的程序繼續運行。Cortex-M內核的MCU提供了一個用…

vector向量容器

一、vector向量容器 簡介&#xff1a; Vector向量容器可以簡單的理解為一個數組&#xff0c;它的下標也是從0開始的&#xff0c;使用時可以不用確定大小&#xff0c;但是它可以對于元素的插入和刪除&#xff0c;可以進行動態調整所占用的內存空間&#xff0c;它里面有很多系統…

netsh(二)

netsh 來自微軟的網絡管理看家法寶很多時候&#xff0c;我們可能需要在不同的網絡中工作&#xff0c;一遍又一遍地重復修改IP地址是一件比較麻煩的事。另外&#xff0c;系統崩潰了&#xff0c;重新配置網卡等相關參數也比較煩人&#xff08;尤其是無線網卡&#xff09;。事實上…

java uuid靜態方法_Java UUID getLeastSignificantBits()方法與示例

java uuid靜態方法UUID類getLeastSignificantBits()方法 (UUID Class getLeastSignificantBits() method) getLeastSignificantBits() method is available in java.util package. getLeastSignificantBits()方法在java.util包中可用。 getLeastSignificantBits() method is us…