簡單連接資料庫(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); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/247202.html
標籤:其他
上一篇:Spring Integration系列《3》JMS ActiveMQ實列
下一篇:軟體設計模式---從0到1
