• JAVA与数据库(JDBC)基本操作实战演练


    依托Myeclipse的JAVA编程实战演练

    一、实现目标。

         通过可视化窗口实现系统的账号登录与注册(极简版),账号与密码储存在数据库中。

    二、构建可视化窗口。 

    三、构建数据库。

       构建数据库student,表名login,三列:name,password,email。

       账户:root 密码 1024117065.

    数据库的构建请查阅相关资料。

    四、驱动加载。

    右键工程文件->选择properties->java Build Path ->Add External JARs.....->选择已经下载好的程序包

    五、代码实现。

    1、主函数

    package main;
    import ui.login;
    public class Main {
        /**
         * @param args
         *;
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            login lf =new login();
            lf.setTitle("登录页面");
            lf.setVisible(true);
            lf.setResizable(false);
    //        lf.setBounds(100, 150, 350, 300);
        }
    
    }

    2、登录界面:

    /*
     * login.java
     *
     * Created on __DATE__, __TIME__
     */
    
    package ui;
    
    import java.sql.*;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author  __USER__
     */
    public class login extends javax.swing.JFrame {
    
        /** Creates new form login */
        public login() {
            initComponents();
        }
    
        /** 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.
         */
        //GEN-BEGIN:initComponents
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jFormattedTextField1 = new javax.swing.JFormattedTextField();
            jPasswordField1 = new javax.swing.JPasswordField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            label1 = new java.awt.Label();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("u7528u6237u540d");
    
            jLabel2.setText("u5bc6u7801");
    
            jFormattedTextField1
                    .addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jFormattedTextField1ActionPerformed(evt);
                        }
                    });
    
            jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jPasswordField1ActionPerformed(evt);
                }
            });
    
            jButton1.setText("u767bu5f55");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("u6ce8u518c");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            label1.setFont(new java.awt.Font("Dialog", 0, 36));
            label1.setText("u767bu5f55u9875u9762");
    
            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(66, 66, 66)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(jLabel2)
                                                    .addComponent(jLabel1))
                                    .addPreferredGap(
                                            javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                                    false)
                                                    .addGroup(
                                                            javax.swing.GroupLayout.Alignment.LEADING,
                                                            layout.createSequentialGroup()
                                                                    .addComponent(
                                                                            jButton1)
                                                                    .addPreferredGap(
                                                                            javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                            Short.MAX_VALUE)
                                                                    .addComponent(
                                                                            jButton2))
                                                    .addComponent(
                                                            jPasswordField1,
                                                            javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(
                                                            jFormattedTextField1,
                                                            javax.swing.GroupLayout.Alignment.LEADING,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            191, Short.MAX_VALUE))
                                    .addContainerGap(115, Short.MAX_VALUE))
                    .addGroup(
                            javax.swing.GroupLayout.Alignment.TRAILING,
                            layout.createSequentialGroup()
                                    .addGap(128, 128, 128)
                                    .addComponent(label1,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            191, Short.MAX_VALUE)
                                    .addGap(94, 94, 94)));
            layout.setVerticalGroup(layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(
                            layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(label1,
                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(11, 11, 11)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(jLabel1)
                                                    .addComponent(
                                                            jFormattedTextField1,
                                                            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.TRAILING)
                                                    .addComponent(jLabel2)
                                                    .addComponent(
                                                            jPasswordField1,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(jButton1)
                                                    .addComponent(jButton2))
                                    .addContainerGap(39, Short.MAX_VALUE)));
    
            pack();
        }// </editor-fold>
        //GEN-END:initComponents
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            log lf = new log();
            lf.setTitle("注册页面");
            lf.setVisible(true);
            lf.setResizable(false);
        }
    
        private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    
        private void jFormattedTextField1ActionPerformed(
                java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            try {
    
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager
                        .getConnection("jdbc:mysql://localhost:1234/student",
                                "root", "1024117065");
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("select * from login");
    
                String na = jFormattedTextField1.getText();
                String jp = new String(jPasswordField1.getPassword());
                boolean find = false;
                while (rs.next()) {
                    if (na.equals(rs.getString("name"))
                            && jp.equals(rs.getObject("password"))) {
                        find = true;
                        this.dispose();
    
                        RuFram sf = new RuFram();
                        sf.setTitle("主页面");
                        sf.setVisible(true);
                        sf.setResizable(false);
                    }
                }
                if (find == false) {
    
                    JOptionPane.showMessageDialog(null, "用户" + "不存在或者密码错误!");
    
                }
    
            } catch (Exception e) {
                System.out.println("err" + e.getMessage());
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new login().setVisible(true);
                }
            });
        }
    
        //GEN-BEGIN:variables
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JFormattedTextField jFormattedTextField1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPasswordField jPasswordField1;
        private java.awt.Label label1;
        // End of variables declaration//GEN-END:variables
    
    }

    3、注册界面:

    /*
     * log.java
     *
     * Created on __DATE__, __TIME__
     */
    
    package ui;
    
    import java.sql.*;
    
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author  __USER__
     */
    public class log extends javax.swing.JFrame {
    
        /** Creates new form log */
        public log() {
            initComponents();
        }
    
        /** 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.
         */
        //GEN-BEGIN:initComponents
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jTextField3 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            label1 = new java.awt.Label();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("u7528u6237u540d");
    
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("u5bc6u7801");
    
            jLabel3.setText("u90aeu7bb1");
    
            jButton1.setText("u786eu5b9a");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("u5173u95ed");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            label1.setFont(new java.awt.Font("Dialog", 0, 24));
            label1.setText("u6ce8u518cu9875u9762");
    
            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(61, 61, 61)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.LEADING,
                                                    false)
                                                    .addGroup(
                                                            layout.createSequentialGroup()
                                                                    .addComponent(
                                                                            jButton1)
                                                                    .addPreferredGap(
                                                                            javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                            Short.MAX_VALUE)
                                                                    .addComponent(
                                                                            jButton2))
                                                    .addGroup(
                                                            layout.createSequentialGroup()
                                                                    .addGroup(
                                                                            layout.createParallelGroup(
                                                                                    javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                    false)
                                                                                    .addComponent(
                                                                                            jLabel3,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            Short.MAX_VALUE)
                                                                                    .addComponent(
                                                                                            jLabel2,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            Short.MAX_VALUE)
                                                                                    .addComponent(
                                                                                            jLabel1,
                                                                                            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,
                                                                                    false)
                                                                                    .addComponent(
                                                                                            jTextField3)
                                                                                    .addComponent(
                                                                                            jTextField2)
                                                                                    .addComponent(
                                                                                            jTextField1,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            118,
                                                                                            Short.MAX_VALUE)
                                                                                    .addComponent(
                                                                                            label1,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            Short.MAX_VALUE))))
                                    .addContainerGap(94, Short.MAX_VALUE)));
            layout.setVerticalGroup(layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(
                            layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(label1,
                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(30, 30, 30)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(jLabel1)
                                                    .addComponent(
                                                            jTextField1,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(
                                            javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(
                                                            jTextField2,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel2))
                                    .addGap(18, 18, 18)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(
                                                            jTextField3,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel3))
                                    .addGap(34, 34, 34)
                                    .addGroup(
                                            layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE)
                                                    .addComponent(jButton1)
                                                    .addComponent(jButton2))
                                    .addContainerGap(37, Short.MAX_VALUE)));
    
            pack();
        }// </editor-fold>
        //GEN-END:initComponents
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            dispose();
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
            /**
             * 1:向数据库中添加数据
             * @author biexiansheng
             *
             */
    
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager
                        .getConnection("jdbc:mysql://localhost:1234/student",
                                "root", "1024117065");
                System.out.println("加载数据库驱动成功");
                Statement stmt = conn.createStatement();//创建一个Statement对象
                //                    ResultSet rs = stmt.executeQuery("select * from login");
    
                //建立数据库连接,获得连接对象conn(抛出异常即可)
                System.out.println("连接数据库成功");
                //生成一条mysql语句
                ResultSet rs = stmt.executeQuery("select * from login");
                String name = jTextField1.getText();
                String password = jTextField2.getText();
                String email = jTextField3.getText();
                boolean find = true;
                while (rs.next()) {
                    if (name.equalsIgnoreCase(rs.getString("name"))) {
                        find = false;
                    }//查找name是否存在于数据库
                }
                if (find) {
                    String sql = "INSERT INTO login(name,password,email) VALUES ('"
                            + name + "','" + password + "','" + email + "')";//定义sql语句
                    stmt.executeUpdate(sql);//执行sql语句
                    System.out.println("插入到数据库成功");
                    conn.close();
                    System.out.println("关闭数据库成功");
                    JOptionPane.showMessageDialog(null, "注册成功");//弹出窗口
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "用户" + name + "已经被注册");
    
                }
    
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new log().setVisible(true);
                }
            });
        }
    
        //GEN-BEGIN:variables
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private java.awt.Label label1;
        // End of variables declaration//GEN-END:variables
    
    }

    六、TIP:

    若需要程序源文件以及相关软件,联系QQ1024117065

    部分内容参加本人其他博客。

  • 相关阅读:
    SQL 窗口函数
    时间序列模型(三):指数平滑法
    时间序列模型(二):移动平均法(MA)
    时间序列模型(一):模型概述
    时间序列分析
    分类数据和顺序数据转换为标志变量
    数据标准化
    指数加权移动平均法(EWMA)
    适用于多品种情况的回归控制图
    SQL NOT NULL 约束
  • 原文地址:https://www.cnblogs.com/shdwin/p/10219926.html
Copyright © 2020-2023  润新知