How to display mysql in java
Use java's swing component to draw the table, realize the functions of "add", "delete", "save" and "exit", and connect it to the mysql database.
You can extract the data from the table in the database and display it on the form containing the table, or you can write the modified content in the table into the database table.
I used two classes to implement the above functions, one of which is MyFrame and the other is PutinStorage.
The specific code is as follows (the following codes are complete codes and have been successfully tested):
PutinStorage class:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.Vector; import javax.swing.JOptionPane; public class PutinStorage { // 得到数据库表数据 public static Vector getRows(){ String sql_url = "jdbc:mysql://localhost:3306/haha"; //数据库路径(一般都是这样写),test是数据库名称 String name = "root"; //用户名 String password = "123456"; //密码 Connection conn; PreparedStatement preparedStatement = null; Vector rows = null; Vector columnHeads = null; try { Class.forName("com.mysql.jdbc.Driver"); //连接驱动 conn = DriverManager.getConnection(sql_url, name, password); //连接数据库 // if(!conn.isClosed()) // System.out.println("成功连接数据库"); preparedStatement = conn.prepareStatement("select * from aa"); ResultSet result1 = preparedStatement.executeQuery(); if(result1.wasNull()) JOptionPane.showMessageDialog(null, "结果集中无记录"); rows = new Vector(); ResultSetMetaData rsmd = result1.getMetaData(); while(result1.next()){ rows.addElement(getNextRow(result1,rsmd)); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("未成功加载驱动。"); e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("未成功打开数据库。"); e.printStackTrace(); } return rows; } // 得到数据库表头 public static Vector getHead(){ String sql_url = "jdbc:mysql://localhost:3306/haha"; //数据库路径(一般都是这样写),test是数据库名称 String name = "root"; //用户名 String password = "123456"; //密码 Connection conn; PreparedStatement preparedStatement = null; Vector columnHeads = null; try { Class.forName("com.mysql.jdbc.Driver"); //连接驱动 conn = DriverManager.getConnection(sql_url, name, password); //连接数据库 // if(!conn.isClosed()) // System.out.println("成功连接数据库"); preparedStatement = conn.prepareStatement("select * from aa"); ResultSet result1 = preparedStatement.executeQuery(); boolean moreRecords = result1.next(); if(!moreRecords) JOptionPane.showMessageDialog(null, "结果集中无记录"); columnHeads = new Vector(); ResultSetMetaData rsmd = result1.getMetaData(); for(int i = 1; i <p><strong>MyFrame class : </strong></p><pre class="brush:php;toolbar:false">import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import per.tushu.storage.PutinStorage; public class MyFrame extends JFrame{ DefaultTableModel tableModel; // 默认显示的表格 JButton add,del,exit,save; // 各处理按钮 JTable table; // 表格 JPanel panelUP; //增加信息的面板 // 构造函数 public MyFrame(){ this.setBounds(300, 200, 600, 450); // 设置窗体大小 this.setTitle("测试"); // 设置窗体名称 this.setLayout(new BorderLayout()); // 设置窗体的布局方式 // 新建各按钮组件 add = new JButton("增加"); del = new JButton("删除"); save = new JButton("保存"); exit = new JButton("退出"); panelUP = new JPanel(); // 新建按钮组件面板 panelUP.setLayout(new FlowLayout(FlowLayout.LEFT)); // 设置面板的布局方式 // 将各按钮组件依次添加到面板中 panelUP.add(add); panelUP.add(del); panelUP.add(save); panelUP.add(exit); // 取得haha数据库的aa表的各行数据 Vector rowData = PutinStorage.getRows(); // 取得haha数据库的aa表的表头数据 Vector columnNames = PutinStorage.getHead(); // 新建表格 tableModel = new DefaultTableModel(rowData,columnNames); table = new JTable(tableModel); JScrollPane s = new JScrollPane(table); // 将面板和表格分别添加到窗体中 this.add(panelUP,BorderLayout.NORTH); this.add(s); // 事件处理 MyEvent(); this.setVisible(true); // 显示窗体 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体可关闭 } // 事件处理 public void MyEvent(){ // 增加 add.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // 增加一行空白区域 tableModel.addRow(new Vector()); } }); // 删除 del.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub // 删除指定行 int rowcount = table.getSelectedRow(); if(rowcount >= 0){ tableModel.removeRow(rowcount); } } }); /** * 保存 * 我的解决办法是直接将aa表中的全部数据删除, * 将表格中的所有内容获取到, * 然后将表格数据重新写入aa表 */ save.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { int column = table.getColumnCount(); // 表格列数 int row = table.getRowCount(); // 表格行数 // value数组存放表格中的所有数据 String[][] value = new String[row][column]; for(int i = 0; i <p><strong>When executing the above code, the initially displayed form is as follows: </strong></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/697/529/372/1558093588142542.png" class="lazy" title="1558093588142542.png" alt="How to display mysql in java"></p><p><strong> Click the Add button and write the content that needs to be added (I added it three times) as shown below: </strong></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/488/336/216/1558093605391393.png" class="lazy" title="1558093605391393.png" alt="How to display mysql in java"></p><p><strong>Click the Delete button to delete the specified row (I deleted Lines 2 and 4), as shown below: </strong></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/191/581/869/1558093624953964.png" class="lazy" title="1558093624953964.png" alt="How to display mysql in java"></p><p>Click the save button and you will find that the window is also closed. You can re-execute the code and you will find that the table page that appears is the same as the picture above. </p><p>Click the exit button to close the window. </p>
The above is the detailed content of How to display mysql in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
