📄 login.java
字号:
/*
* Login.java
*
* Created on 2008年3月28日, 下午4:57
*/
package pda;
import java.awt.Cursor;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class Login extends javax.swing.JFrame {
/** Creates new form Login */
public Login() {
super("登陆界面");
initComponents();
getContentPane().setBackground(java.awt.Color.CYAN);
}
/** 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="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
username = new javax.swing.JLabel();
password = new javax.swing.JLabel();
userName = new javax.swing.JTextField();
login = new javax.swing.JButton();
passWord = new javax.swing.JPasswordField();
help = new javax.swing.JLabel();
newuser = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
username.setFont(new java.awt.Font("黑体", 0, 16));
username.setText("用户名:");
username.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
usernameMouseClicked(evt);
}
});
password.setFont(new java.awt.Font("黑体", 0, 16));
password.setText("密 码:");
login.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tu1.jpg"))); // NOI18N
login.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
loginMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
loginMouseExited(evt);
}
});
login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});
help.setText("帮助");
help.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
helpMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
helpMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
helpMouseExited(evt);
}
});
newuser.setText("注册");
newuser.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
newuserMouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
newuserMouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
newuserMouseExited(evt);
}
});
jLabel1.setFont(new java.awt.Font("黑体", 0, 20));
jLabel1.setText("用 户 登 录");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(password)
.addComponent(username))
.addGap(8, 8, 8)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(passWord, 0, 0, Short.MAX_VALUE)
.addComponent(userName, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
.addContainerGap(79, Short.MAX_VALUE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(328, Short.MAX_VALUE)
.addComponent(newuser)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(help)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addComponent(jLabel1)
.addContainerGap(131, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(help)
.addComponent(newuser, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5, 5, 5)
.addComponent(jLabel1)
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(username))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(password)
.addComponent(passWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
}// </editor-fold>//GEN-END:initComponents
private void helpMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_helpMouseClicked
// TODO 将在此处添加您的处理代码:
/*try {
Runtime ce=Runtime.getRuntime();
ce.exec("notepad src/file/help.txt");
} catch (IOException ex) {
ex.printStackTrace();
}*/
Help h=new Help();
h.setVisible(true);
}//GEN-LAST:event_helpMouseClicked
private void usernameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_usernameMouseClicked
// TODO 将在此处添加您的处理代码:
}//GEN-LAST:event_usernameMouseClicked
private void newuserMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newuserMouseClicked
// TODO 将在此处添加您的处理代码:
this.setVisible(false);
AddUser au=new AddUser();
au.setVisible(true);
}//GEN-LAST:event_newuserMouseClicked
private void loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginActionPerformed
// TODO 将在此处添加您的处理代码:
loginF();
}//GEN-LAST:event_loginActionPerformed
private void newuserMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newuserMouseEntered
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
}//GEN-LAST:event_newuserMouseEntered
private void helpMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_helpMouseEntered
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
}//GEN-LAST:event_helpMouseEntered
private void newuserMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newuserMouseExited
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_newuserMouseExited
private void helpMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_helpMouseExited
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_helpMouseExited
private void loginMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginMouseEntered
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
}//GEN-LAST:event_loginMouseEntered
private void loginMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginMouseExited
// TODO add your handling code here:
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_loginMouseExited
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
Login l=new Login();
l.setVisible(true);
}
void loginF(){
file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
un=userName.getText();
pw=new String(passWord.getPassword(),0,passWord.getPassword().length);
//sql="SELECT * FROM user WHERE username="+"'"+un+"' and password='"+pw+"'";//Statement方法做
// ' or '1'='1
sql="SELECT * FROM user WHERE username=? and password=?";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:pda");//用数据源方法做。
//Connection con=DriverManager.getConnection(path);
ResultSet rs;
/*Statement stmt=con.createStatement();
rs=stmt.executeQuery(sql);*/
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,un);
pst.setString(2,pw);
rs=pst.executeQuery();
if(rs.next())
{
this.setVisible(false);
new Main().setVisible(true);
}
else
{
JOptionPane.showMessageDialog(this,"用户名或密码错误!");
userName.setText(null);passWord.setText(null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel help;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton login;
private javax.swing.JLabel newuser;
private javax.swing.JPasswordField passWord;
private javax.swing.JLabel password;
private javax.swing.JTextField userName;
private javax.swing.JLabel username;
// End of variables declaration//GEN-END:variables
String un,pw,sql,path;
File file;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -