-
登陸界面
登陸界面 - 代碼
GUI界面
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import javax.imageio.ImageIO;
import javax.security.auth.login.LoginContext;
import javax.swing.*;
import com.mysql.jdbc.PreparedStatement;
import UserMysql.connectLog_Mysql;
public class userLog_Window extends connectLog_Mysql { //此類中只調(diào)用WindowLog里面的方法
public static void main(String args[]){
//調(diào)用WindowLogin里面的方法
WindowLogin win = new WindowLogin();
}
}
class WindowLogin extends JFrame implements ActionListener{ //JFrame的子類
//聲明組件
JLabel jlpic = new JLabel();
JFrame jf ;
JLabel Title = new JLabel("用戶管理系統(tǒng)");
JLabel Name = new JLabel("賬號:");
JTextField textName = new JTextField(10);
JLabel Passwd = new JLabel("密碼:");
JTextField textPasswd = new JTextField(10);
JButton btnLog = new JButton("登錄");
public WindowLogin(){ //主要設(shè)置窗口屬性,調(diào)用函數(shù)實(shí)現(xiàn)組件
//基本設(shè)置:
//1.窗口聲明
jf = new JFrame("用戶登錄");
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setSize(1000,800);
jf.setLocation(500,200);
//2.面板聲明
JPanel panel = new JPanel();
//設(shè)置面板透明
panel.setOpaque(false);
//3.窗口中添加面板
jf.add(panel);
//4.添加組件到面板
placeComponents(panel);
//調(diào)用函數(shù)
initFrame();
}
private void placeComponents(JPanel panel) { //對組件的具體操作
//設(shè)置為布局為null,這樣才可以給他絕對定位組件
panel.setLayout(null);
//add添加功能組件進(jìn)去
Title.setBounds(250, 130, 300, 35);
Title.setForeground(Color.pink);
Title.setFont(new Font("楷體",1,30));
panel.add(Title);
Name.setBounds(200,200,120,25); //一定要先設(shè)置他的位置,否則一直都不會顯示噢
Name.setFont(new Font("楷體",NORMAL,25));
panel.add(Name);
textName.setBounds(280,200,165,30);
panel.add(textName);
Passwd.setBounds(200,250,80,25);
Passwd.setFont(new Font("楷體",NORMAL,25));
panel.add(Passwd);
textPasswd.setBounds(280,250,165,30);
panel.add(textPasswd);
btnLog.setBounds(320,300,70,30);
btnLog.setBackground(Color.pink);
btnLog.setFont(new Font("楷體",NORMAL,18));
panel.add(btnLog);
//給按鈕添加監(jiān)聽器
btnLog.addActionListener(this);
}
public void initFrame() {
ImageIcon icon = new ImageIcon("src\\pink.jpg");
icon.setImage(icon.getImage().getScaledInstance(900,
500, Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jf.add(jlpic);
jf.pack();
}
@Override
public void actionPerformed(ActionEvent e) {
//點(diǎn)擊登錄后,調(diào)用login方法將用戶輸入的內(nèi)容與mysql中存儲的內(nèi)容
if(e.getSource() == btnLog) {
login();
}
}
//和Mysql數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行聯(lián)系
private void login() {
connectLog_Mysql connect = new connectLog_Mysql();
//獲取用戶輸入的用戶名和密碼
String username = textName.getText();
String password = textPasswd.getText();
System.out.println("用戶輸入的:"+username + ","+ password);
//在mysql中搜索這個(gè)用戶名,不存在則輸出用戶不存在
//若用戶名存在,對照密碼,密碼正確則登錄ok,不正確則輸出密碼不對
if (connect.compare(username, password)) {
JOptionPane.showMessageDialog(null, "登錄成功!");
// super.setVisible(false);
jf.dispose();
new WindowChoose();
}
}
}
- 連接MySQL
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import com.mysql.jdbc.PreparedStatement;
public class connectLog_Mysql {
//對應(yīng)Window窗口中調(diào)用的數(shù)據(jù)庫操作方法
public Boolean compare(String username, String password) {
boolean m = false;
try {
//要去掉mysql和jdbc之間的cj!!!
Class.forName("com.mysql.jdbc.Driver");//根據(jù)報(bào)錯(cuò)信息修改
String url = "jdbc:mysql://localhost:3307/users?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";//鏈接的mysql
String user = "root";
String passwd = "123456";
Connection con = DriverManager.getConnection(url,user,passwd);//處理異常
System.out.println("Mysql連接成功" + con.toString());//測試是否連接成功
//執(zhí)行sql語句
Statement sql;
ResultSet rs;
try {
//此方法創(chuàng)建用于執(zhí)行靜態(tài)SQL 語句并返回它所生成結(jié)果的對象
sql = con.createStatement();
//定義所有查詢的語句
String searchALL = "select * from admininfo";
//執(zhí)行要查詢的語句
rs = sql.executeQuery(searchALL);
while(rs.next()) {
String username2 = rs.getString(2);
String password3 = rs.getString(3);
System.out.println("數(shù)據(jù)庫中"+username2 + ":" + password3);
if (password.equals(password3) && username.equals(username2)) {
m = true;
break;
} else if(rs.isLast()){
JOptionPane.showMessageDialog(null, "用戶名或密碼錯(cuò)誤!");
break;
}
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("error:數(shù)據(jù)庫連接出錯(cuò)");
}
return m;
}
public static void main(String args[]){
connectSearchInfo_Mysql con = new connectSearchInfo_Mysql();
}
}
