简单连接数据库(swing)

简单连接数据库(swing)

  • 配置文件

    driver=com.mysql.jdbc.Driver
    username=root
    passwd=root
    url=jdbc:mysql://localhost:3306/data_employee
    
  • 工具类(用户获取数据库连接、释放资源)

    package shixun.zuizhong;
    
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.URL;
    import java.sql.*;
    import java.util.Properties;
    
    public class Mysql_driver_utils {
           
    
        static String url,username,passwd;
    
    
        static {
           
    
            try {
           
                ClassLoader classLoader = Mysql_driver_utils.class.getClassLoader();
                URL resource = classLoader.getResource("mysql_driver.properties");
                String path = resource.getPath();
    
                Properties properties = new Properties();
                properties.load(new FileReader(path));
    
                url = properties.getProperty("url");
                username = properties.getProperty("username");
                passwd = properties.getProperty("passwd");
    
                String driver = properties.getProperty("driver");
                Class.forName(driver);
            } catch (IOException e) {
           
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
           
                e.printStackTrace();
            }
        }
    
    
        public static  Connection getconnetion() throws SQLException {
           
    
            return DriverManager.getConnection(url,username,passwd);
        }
    
        public static void close(Connection connection, Statement statement, ResultSet resultSet){
           
    
            if(resultSet!=null){
           
                try {
           
                    resultSet.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
    
            if(statement!=null){
           
                try {
           
                    resultSet.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
    
            if(connection!=null){
           
                try {
           
                    resultSet.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
    
        }
        public static void close(Connection connection, Statement statement){
           
    
    
            if(statement!=null){
           
                try {
           
                    statement.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
    
            if(connection!=null){
           
                try {
           
                    connection.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
    
        }
        public static void close(Statement statement){
           
            if (statement!=null){
           
                try {
           
                    statement.close();
                } catch (SQLException throwables) {
           
                    throwables.printStackTrace();
                }
            }
        }
    
    }
    
    
  • 界面和事件

    package shixun.zuizhong;
    
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class Register_employee {
           
        public static void main(String[] args) {
           
    
            JFrame jFrame = new JFrame("员工信息注册");
            jFrame.setSize(250,400);
            jFrame.setVisible(true);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
            JLabel username = new JLabel("用户名:");
            JTextField user_n = new JTextField(20);
    
            JLabel passwd = new JLabel("密码:");
            JTextField passwd_p = new JTextField(20);
    
            JButton register_btn = new JButton("注册");
    
    
            JPanel jPanel = new JPanel();
    
            jPanel.add(username);
            jPanel.add(user_n);
            jPanel.add(passwd);
            jPanel.add(passwd_p);
    
            jPanel.add(register_btn);
    
            jFrame.add(jPanel);
    
            ActionListener actionListener_re = new ActionListener() {
           
                @Override
                public void actionPerformed(ActionEvent e) {
           
                    String user_text = user_n.getText();
                    String passwd_text = passwd_p.getText();
    
                    Connection connection = null;
                    PreparedStatement preparedStatement = null;
                    PreparedStatement preparedStatement2 = null;
                    ResultSet resultSet = null;
                    try {
           
                        connection = Mysql_driver_utils.getconnetion();  //获取数据库连接对象
                        connection.setAutoCommit(false);  //开启事务 手动提交
                        String sql_str = "select * from table_employee where username=?";
    
                        preparedStatement = connection.prepareStatement(sql_str);
                        preparedStatement.setString(1,user_text);
                        //preparedStatement.setString(2,passwd_text);
                        resultSet = preparedStatement.executeQuery();
    
                        if(resultSet.next()){
           
                            user_n.setText("账户已存在");
                        }else {
             //没有记录,注册成功 账号密码写入数据库
                            String sqlinster_str = "insert into table_employee(username,passwd) values (?,?)";
                            preparedStatement2 = connection.prepareStatement(sqlinster_str);
                            preparedStatement2.setString(1,user_text);
                            preparedStatement2.setString(2,passwd_text);
                            int i = preparedStatement2.executeUpdate();
                            if(i>0){
           
                                user_n.setText("注册成功!");
                            }else {
           
                                user_n.setText("注册失败!");
                            }
                        }
                        connection.commit();  //提交事务
                    } catch (SQLException throwables) {
           
                        throwables.printStackTrace();
                        try {
           
                            connection.rollback();  //事务回滚
                        } catch (SQLException sqlException) {
           
                            sqlException.printStackTrace();
                        }
                    }finally {
           //释放资源
                        Mysql_driver_utils.close(connection,preparedStatement,resultSet);
                        Mysql_driver_utils.close(preparedStatement2);
                    }
                }
            };
    
            register_btn.addActionListener(actionListener_re);
        }
    }
    
    

你可能感兴趣的:(简单连接数据库(swing))