⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adduser.java

📁 基于java开发的个人数字助理
💻 JAVA
字号:
/*
 * AddUser.java
 *
 * Created on 2008年4月21日, 上午12:44
 */

package pda;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;

/**
 *
 * @author  lnn
 */
public class AddUser extends javax.swing.JFrame {
    
    /** Creates new form AddUser */
    public AddUser() {
        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=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        userNameLabel = new javax.swing.JLabel();
        passWordLabel = new javax.swing.JLabel();
        unTextField = new javax.swing.JTextField();
        pwTextField = new javax.swing.JTextField();
        userLabel = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();

        userNameLabel.setFont(new java.awt.Font("黑体", 0, 16));
        userNameLabel.setText("\u7528\u6237\u540d\uff1a");

        passWordLabel.setFont(new java.awt.Font("黑体", 0, 16));
        passWordLabel.setText("\u5bc6  \u7801\uff1a");

        userLabel.setFont(new java.awt.Font("黑体", 0, 20));
        userLabel.setText("\u7528  \u6237  \u6ce8  \u518c");

        addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tu2.JPG")));
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(140, Short.MAX_VALUE)
                .addComponent(userLabel)
                .addGap(120, 120, 120))
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passWordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(userNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(pwTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                        .addComponent(unTextField)))
                .addContainerGap(78, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(userLabel)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userNameLabel)
                    .addComponent(unTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passWordLabel)
                    .addComponent(pwTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66))
        );
        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 addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
// TODO 将在此处添加您的处理代码:
        addUser();
        this.setVisible(false);
        JOptionPane.showMessageDialog(null, "注册成功!");
        Login l=new Login();
        l.setVisible(true);
    }//GEN-LAST:event_addButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        AddUser au=new AddUser();
        au.setVisible(true);
    }*/

    public void addUser(){
        file=new File("src/database/pda.mdb");//经典所在:不用配置数据源
        path="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+file.getAbsolutePath();//获取绝对路径
        un=unTextField.getText();
        pw=pwTextField.getText();
        sql="INSERT INTO user VALUES(?,?)";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:pda");
            //Connection con=DriverManager.getConnection(path);
            PreparedStatement pst=con.prepareStatement(sql);
            pst.setString(1,un);
            pst.setString(2,pw);
            pst.executeUpdate();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JLabel passWordLabel;
    private javax.swing.JTextField pwTextField;
    private javax.swing.JTextField unTextField;
    private javax.swing.JLabel userLabel;
    private javax.swing.JLabel userNameLabel;
    // 变量声明结束//GEN-END:variables
    String un,pw,sql,path;
    File file;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -