📜  Java程序在JDBC中以表格格式输出查询结果

📅  最后修改于: 2022-05-13 01:55:01.285000             🧑  作者: Mango

Java程序在JDBC中以表格格式输出查询结果

JDBC(Java Database Connectivity)是Java编程语言与oracle、SQL等各种数据库之间的标准API(应用程序接口),连接前端用于与用户交互,后端用于存储数据。

程序:

  1. 创建数据库
  2. 连接类
  3. 以表格格式输出结果。
    • 在同一个包中,右键单击它并打开一个 JFrame 表单并命名
    • 添加表格摆动控件(拖放)和“查看”按钮
    • 单击“源”选项卡并导入以下库
    • 如果单独的 jar 文件不起作用,请下载一个 rs2xml.JAR 文件,另外导入DbUtils
    • 转到设计选项卡并双击“查看”按钮以编写用于 jdbc 连接的程序并获取结果。
  4. 通过双击“查看”按钮来编写代码,注意不要在主方法中写入。
  5. 显示输出

执行:

第一步:使用sqlyog创建一个数据库并在其中创建一些表并在其中填充数据以输出 表的内容。

第 2 步:连接类

  • 打开IDE。 Netbeans 是为了方便说明,因为它已经包含了预先安装的所需 jar 文件。对于任何其他 IDE,首先,在创建连接类之前导入所需的 4 个 jar 文件。
  • 在里面创建一个包,以便在数据库和程序之间建立连接。
  • 在包内,打开一个新的Java文件并创建一个连接类,以便可以在main(App类)中访问其对象以进行JDBC连接
  • 最后,使用连接保存文件名。Java。

例子

Java
// Java Program to Output Query Results in Tabular Format in
// JDBC
 
// Importing database classes for
// handling sql exception and for jdbc connectivity
// name of database here is mysql
import java.sql.*;
 
// Connection class
public class connection {
 
    // Initially setting object to NULL in order to
    // avoid any garbage value isse
    Connection con = null;
 
    public static Connection connectDB()
 
    {
        // Try block to check all exceptions
        try {
 
            // Loading driver using forName() method
            Class.forName("com.mysql.jdbc.Driver");
 
            // Registering driver using DriverManager
            // root is the username
            // 1234 is the password
            // Can set your own username and password
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/hotelman",
                "root", "1234");
 
            // eturning connection object which later on
            // to be used in Main/App class
            return con;
        }
 
        // Catch block to handle DB exceptions
        catch (SQLException e) {
 
            // Print the exception occured
            System.out.println(e);
        }
    }
}


Java
// Java Program for for jdbc connection
// and for obtaining the result
 
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
the above code is con = connection.connectDB();
 
// Query to display details of all customers
String sql = "select * from cuslogin";
 
// Try block to check for exceptions
try {
    ps = con.prepareStatement(sql);
 
    // result is stored in rs
    rs = ps.executeQuery();
 
    // send the result to the table
    // Here, jTable1 is the name of the tablular format
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
 
// Catch block to handle exceptions
catch (Exception e) {
 
    // Display exception message as in dialog box
    JOptionPane.showMessageDialog(null, e);
}


Java
// Importing input output java files
import java.io.*;
 
// Class
class GFG {
 
    // Main friver method
    public static void main(String[] args)
    {
 
        // Assigning NULL to connection object
        Connection con = null;
 
        PreparedStatement ps = null;
 
        ResultSet rs = null;
 
        // For jdbc connection
        con = connection.connectDB();
 
        // Query to display details of all customers
        String sql = "select * from cuslogin";
 
        try {
 
            ps = con.prepareStatement(sql);
 
            // Result is stored in rs
            rs = ps.executeQuery();
 
            // Send the result to the table
            jTable1.setModel(
                DbUtils.resultSetToTableModel(rs));
 
            // Here, jTable1 is name of the tablular format
        }
       
      // Catch block to handle if exception occured
        catch (Exception e) {
             
            // Display exception message in dialog block
            JOptionPane.showMessageDialog(null, e);
        }
    }
}



第 3 步:以表格格式输出结果。

3.1在同一个包内,右键单击它并打开一个JFrame表单并给出您选择的名称(如下所示)

3.2添加表格摆动控件(拖放)和“查看”按钮(单击时将在表格中显示结果)

3.3单击 Source 选项卡并导入以下库

import java.sql.*; //for handling jdbc related syntax
import javax.swing.JOptionPane;  //for showing message
import net.proteanit.sql.DbUtils; //for displaying the result of query to table form

3.4下载一个 rs2xml.JAR 文件,如果单独的 jar 文件不起作用,请另外导入DbUtils 。现在,只需按照以下步骤解压并上传 jar 文件:

3.5进入设计选项卡,双击“查看”按钮,在函数内部编写对应的代码,程序如下:

Java

// Java Program for for jdbc connection
// and for obtaining the result
 
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
the above code is con = connection.connectDB();
 
// Query to display details of all customers
String sql = "select * from cuslogin";
 
// Try block to check for exceptions
try {
    ps = con.prepareStatement(sql);
 
    // result is stored in rs
    rs = ps.executeQuery();
 
    // send the result to the table
    // Here, jTable1 is the name of the tablular format
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
 
// Catch block to handle exceptions
catch (Exception e) {
 
    // Display exception message as in dialog box
    JOptionPane.showMessageDialog(null, e);
}


3.5通过双击“查看”按钮来编写代码,注意不要在 main 方法中写入。

例子

Java

// Importing input output java files
import java.io.*;
 
// Class
class GFG {
 
    // Main friver method
    public static void main(String[] args)
    {
 
        // Assigning NULL to connection object
        Connection con = null;
 
        PreparedStatement ps = null;
 
        ResultSet rs = null;
 
        // For jdbc connection
        con = connection.connectDB();
 
        // Query to display details of all customers
        String sql = "select * from cuslogin";
 
        try {
 
            ps = con.prepareStatement(sql);
 
            // Result is stored in rs
            rs = ps.executeQuery();
 
            // Send the result to the table
            jTable1.setModel(
                DbUtils.resultSetToTableModel(rs));
 
            // Here, jTable1 is name of the tablular format
        }
       
      // Catch block to handle if exception occured
        catch (Exception e) {
             
            // Display exception message in dialog block
            JOptionPane.showMessageDialog(null, e);
        }
    }
}


输出:

  • 运行Java文件后

  • 点击查看按钮,输出如下