?? loginjframe.java
字號:
/*
* LoginJFrame.java
*/
package playermanager;
import java.sql.*;
import javax.swing.JOptionPane;
public class LoginJFrame extends javax.swing.JFrame {
/** Creates new form LoginJFrame */
public LoginJFrame() {
initComponents();
}
public void validID() {
String str1, str2;
str1 = jTextField1.getText();
str2 = jPasswordField1.getText();
try {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Driver driver = (Driver) Class.forName(driverName).newInstance();
//連接的數據庫是名為“data“的數據庫
//登錄名是”sa“密碼是“123”
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;", "sa", "123");
con.setCatalog("data"); //數據庫名稱
//執行動態的sql語句
PreparedStatement pstmt = con.prepareStatement("select name,password from usernp where name=?");
//將用戶的名字添加到具體的sql語句中
pstmt.setString(1, str1);
//使用ResultSet中的方法executeQuery()來完成sql語句的執行
ResultSet res = pstmt.executeQuery();
if ((!res.next()) || res.getString("password") == null) {
JOptionPane.showMessageDialog(this, "用戶名錯誤", "錯誤提示",
JOptionPane.ERROR_MESSAGE);
}else {
if ( !(res.getString("password").equals(str2)))
{
JOptionPane.showMessageDialog(this, "密碼錯誤", "錯誤提示",JOptionPane.ERROR_MESSAGE);
}
else{
loader();
this.setVisible(false);
}
}
pstmt.close();
con.close();
}catch (InstantiationException e) {
System.out.println(e.getMessage());
}catch (IllegalAccessException e) {
System.out.println(e.getMessage());
}catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}catch (SQLException edd) {
edd.printStackTrace() ;
System.out.println(edd.getMessage());
}
}
public void loader() {
try {
MainJFrame hh=new MainJFrame();
hh.setVisible(true);
}
catch (Exception ew) {
System.out.println(ew.getMessage());
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代碼 ">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("\u7403\u5458\u7ba1\u7406\u7cfb\u7edf\u7ba1\u7406\u5458\u767b\u9646");
setResizable(false);
jLabel1.setText("\u7528\u6237\u540d \uff1a ");
jLabel2.setText("\u5bc6\u7801 \uff1a ");
jButton1.setText("\u767b\u9646 ");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("\u53d6\u6d88");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(79, 79, 79)
.addComponent(jButton1)
.addGap(98, 98, 98)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))))
.addGap(78, 78, 78))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(35, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// 登陸
validID();
// this.setVisible(false);
// MainJFrame hh=new MainJFrame();
//hh.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// 退出
System.exit(0);
}//GEN-LAST:event_jButton2MouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginJFrame().setVisible(true);
}
});
}
// 變量聲明 - 不進行修改//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// 變量聲明結束//GEN-END:variables
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -