您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Java中如何使用Swing實現通訊錄管理系統的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1.系統功能
登錄系統
查詢信息
新增信息
修改信息
刪除信息
2.環境配置
JDK版本:1.8
Mysql:8.0.13
3.數據庫
/* Navicat Premium Data Transfer Source Server : MySQL Source Server Type : MySQL Source Server Version : 80013 Source Host : localhost:3306 Source Schema : swing_address Target Server Type : MySQL Target Server Version : 80013 File Encoding : 65001 Date: 10/06/2021 23:56:16 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for my_address_book -- ---------------------------- DROP TABLE IF EXISTS `my_address_book`; CREATE TABLE `my_address_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `telephone` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `mail` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `birthday` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `note` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact; -- ---------------------------- -- Records of my_address_book -- ---------------------------- INSERT INTO `my_address_book` VALUES (2, '楊XX', '男', '18911616917', '1@163.com', '20200107', 'XX'); INSERT INTO `my_address_book` VALUES (3, '222', '女', '22', '22', '22', '22'); INSERT INTO `my_address_book` VALUES (4, '1', '女', '1', '1', '1', '1'); -- ---------------------------- -- Table structure for my_address_login -- ---------------------------- DROP TABLE IF EXISTS `my_address_login`; CREATE TABLE `my_address_login` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact; -- ---------------------------- -- Records of my_address_login -- ---------------------------- INSERT INTO `my_address_login` VALUES (1, '1', '1'); SET FOREIGN_KEY_CHECKS = 1;
4.工程截圖
1.登錄頁
2.主頁
3.查詢信息
4.新增信息
5.修改信息
DBConn.java
package com.txl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConn { private static String driverName = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/swing_address?serverTimezone=UTC"; private static String userName = "root"; private static String password = "admin"; private Connection conn; private Statement stmt; public DBConn() { try { Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 連接數據庫 * * @return * @throws SQLException */ public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, userName, password); } /** * 釋放資源 */ public void dispose() { try { if (conn != null) { conn.close(); } if (stmt != null) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
Login.java
package com.txl; import java.awt.FlowLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JPasswordField; public class Login { public static void main(String args[]) { Login l=new Login(); l.showUI(); } public void showUI() { javax.swing.JFrame login=new javax.swing.JFrame(); login.setTitle("登錄通訊錄"); login.setSize(340,230); login.setDefaultCloseOperation(3); login.setLocationRelativeTo(null); login.setResizable(false); java.awt.FlowLayout fl=new java.awt.FlowLayout(FlowLayout.CENTER,5,5); login.setLayout(fl); JLabel labname=new JLabel(); labname.setText("用戶名:"); labname.setPreferredSize(new java.awt.Dimension(60, 60)); login.add(labname); JTextField textname=new JTextField(); textname.setPreferredSize(new java.awt.Dimension(250, 30)); login.add(textname); JLabel labpassword=new JLabel(); labpassword.setText("密 碼:"); labpassword.setPreferredSize(new java.awt.Dimension(60, 60)); login.add(labpassword); JPasswordField jp=new JPasswordField(); jp.setPreferredSize(new java.awt.Dimension(250, 30)); login.add(jp); javax.swing.JButton button=new javax.swing.JButton(); button.setText("登錄"); button.setPreferredSize(new java.awt.Dimension(100, 40)); login.add(button); login.setVisible(true); button.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { DBConn dbconn = new DBConn(); Statement stmt = null; ResultSet rs = null; try { stmt = dbconn.getConnection().createStatement(); rs = stmt.executeQuery("select * from my_address_login where username='"+textname.getText()+"' and password='"+jp.getText()+"'"); if (rs.next()) { new MyAddressBook(); login.dispose(); }else{ JOptionPane.showMessageDialog(null, "用戶名或密碼不正確!!!"); } rs.close(); } catch (SQLException e1) { e1.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } catch (SQLException e1) { e1.printStackTrace(); } } } }); } }
Test.java
package com.txl; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; public class Test { public static void main(String[] args) { DBConn dbconn = new DBConn();;;;; try { for(int i =0 ;i<1000000;i++){ String sql = "insert into student(name, age)values('XXX"+i+"',30)"; PreparedStatement pstmt = dbconn.getConnection().prepareStatement(sql); System.out.println(sql); pstmt.execute(sql); pstmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } }
感謝各位的閱讀!關于“Java中如何使用Swing實現通訊錄管理系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。