之前我用java連接sqlserver,能連接成功,并可以增刪改查,現在想資料庫換成mysql,就把相應的jar換掉了,連接命令也稍微修改了下,顯示可以成功連接上,但是在查詢驗證登錄,卻一直報空指標例外,說下面紅色標注的sql陳述句有問題,還請大家幫忙看一下
登錄界面代碼及運行界面如下:
/*
* 功能:圖書管理系統
* 步驟1:登錄界面的靜態實作
* 步驟2:添加對各個組件的監聽。
* 步驟3:對用戶名和密碼進行驗證。
* author:paranoidyang
*/
package login;
import javax.swing.*;
import home.Home;
import utils.mysql;
import utils.Utils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame implements ActionListener {
// 定義組件
JPanel jp1, jp2, jp3, jp4 = null;
static JTextField userName_jtf = null;
static JPasswordField password_jtf = null;
private JLabel logoLabel, userNameLabel, passwordLabel;
private JButton login_btn, reset_btn;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
public Login() {
Container container = this.getContentPane();
this.setLayout(null);//
// 創建標簽
logoLabel = new JLabel("圖書管理系統");
logoLabel.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/logo.png")));
logoLabel.setFont(Utils.f1);// utils是一個工具類,里面自定義了一個字體的類
logoLabel.setBounds(150, 50, 200, 40);
container.add(logoLabel);
// 創建用戶名標簽
userNameLabel = new JLabel("用戶名:");
userNameLabel.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/user.png")));
userNameLabel.setFont(Utils.f2);
userNameLabel.setBounds(120, 140, 80, 40);
container.add(userNameLabel);
// 創建用戶名輸入框
userName_jtf = new JTextField(10);
userName_jtf.setBounds(220, 148, 150, 20);
container.add(userName_jtf);
// 創建密碼標簽
passwordLabel = new JLabel("密碼:");
passwordLabel.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/password.png")));
passwordLabel.setFont(Utils.f2);
passwordLabel.setBounds(120, 180, 80, 40);
container.add(passwordLabel);
// 創建密碼輸入框
password_jtf = new JPasswordField(10);
password_jtf.setBounds(220, 188, 150, 20);
container.add(password_jtf);
// 創建登錄按鈕
login_btn = new JButton("登錄");
login_btn.setFont(Utils.f2);
login_btn.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/login.png")));
login_btn.setBounds(100, 260, 90, 30);
container.add(login_btn);
// 創建重置按鈕
reset_btn = new JButton("重置");
reset_btn.setFont(Utils.f2);
reset_btn.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("images/reset.png")));
reset_btn.setBounds(300, 260, 90, 30);
container.add(reset_btn);
// 為按鈕設定監聽
login_btn.addActionListener(this);
reset_btn.addActionListener(this);
// 給視窗設定標題
this.setTitle("圖書管理系統");
this.setIconImage(new ImageIcon(this.getClass().getClassLoader().getResource("images/tittle.png")).getImage());
// 設定為表單不可改變大小
this.setResizable(false);
// 設定表單大小
this.setSize(500, 400);
/*
* 實作表單局中顯示,為什么無效果?
* 必須要在設定完表單大小后呼叫才能在中間顯示,否則就會顯示在右下角。引數為null時該方法是將表單的中心移到螢屏的中央,如果一開始未指定表單的大小,那就是一個點,
* 在這時候呼叫此函式,相當于把表單的左上角移到中間,然后在此基礎上再添加其他組件,使表單變大,所以就顯示在右下方了
*/
this.setLocationRelativeTo(null);
// 設定當關閉視窗時,保證JVM也退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 顯示表單
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "登錄") {
// 連接資料庫核對用戶名和密碼
if (!userName_jtf.getText().isEmpty() && !password_jtf.getText().isEmpty()) {
this.uselogin();
} else if (userName_jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "請輸入用戶名", "提示訊息", JOptionPane.WARNING_MESSAGE);
// this.clear();
} else if (password_jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "請輸入密碼", "提示訊息", JOptionPane.WARNING_MESSAGE);
// this.clear();
}
} else if (e.getActionCommand() == "重置") {
clear();
}
}
// 用戶登錄判斷方法 ,檢查賬號密碼時,要注意資料庫中定義的欄位長度跟輸入文本框中的長度可能
// 不一樣,所以要加上trim()去除一切空格格式,然后進行比較,
public void uselogin() {
// 連接資料庫
mysql.ConnectSQL();
mysql.SQLverify(userName_jtf.getText(), password_jtf.getText());
System.out.println(mysql.mypwd);
if (mysql.myuserword.trim().equals(userName_jtf.getText().trim())
&& mysql.mypwd.trim().equals(password_jtf.getText().trim()))// trim()去掉字符序列左邊和右邊的空格,輸出不包含任何格式的字串
{
System.out.println("登錄成功");
JOptionPane.showMessageDialog(null, "登錄成功!", "提示訊息", JOptionPane.WARNING_MESSAGE);
clear();
// 關閉當前界面
dispose();
// 創建一個新界面
Home home = new Home();
} else if (userName_jtf.getText().isEmpty() && password_jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "請輸入用戶名和密碼!", "提示訊息", JOptionPane.WARNING_MESSAGE);
} else if (userName_jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "請輸入用戶名!", "提示訊息", JOptionPane.WARNING_MESSAGE);
} else if (password_jtf.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "請輸入密碼!", "提示訊息", JOptionPane.WARNING_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "用戶名或者密碼錯誤!\n請重新輸入", "提示訊息", JOptionPane.ERROR_MESSAGE);
// 清空輸入框
clear();
}
}
// 清空文本框和密碼框
public static void clear() {
userName_jtf.setText("");
password_jtf.setText("");
}
}

mysql.java是一個連接資料庫并封裝有實作增刪改查方法的類,登錄驗證的相關代碼如下:


uj5u.com熱心網友回復:
我的是的登入界面報錯為什么啊我要吐了轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/269538.html
標籤:其他開發語言
