Java reads database table
package com.easycrud.builder;
import com.easycrud.utils.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
/**
* @BelongsProject: EasyCrud
* @BelongsPackage: com.easycrud.builder
* @Author: xpx
* @Email: 2436846019@qq.com
* @CreateTime: 2023-05-02 18:02
* @Description: 读Table
* @Version: 1.0
*/
public class BuildTable {
private static final Logger logger = LoggerFactory.getLogger(BuildTable.class);
private static Connection conn = null;
/**
* 查表名和表注释
*/
private static String SQL_SHOW_TABLE_STATUS = "show table status";
/**
* 读配置,连接数据库
*/
static {
String driverName = PropertiesUtils.getString("db.driver.name");
String url = PropertiesUtils.getString("db.url");
String user = PropertiesUtils.getString("db.username");
String password = PropertiesUtils.getString("db.password");
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,password);
} catch (Exception e) {
logger.error("数据库连接失败",e);
}
}
/**
* 读取表
*/
public static void getTables() {
PreparedStatement ps = null;
ResultSet tableResult = null;
try{
ps = conn.prepareStatement(SQL_SHOW_TABLE_STATUS);
tableResult = ps.executeQuery();
while(tableResult.next()) {
String tableName = tableResult.getString("name");
String comment = tableResult.getString("comment");
logger.info("tableName:{},comment:{}",tableName,comment);
}
}catch (Exception e){
logger.error("读取表失败",e);
}finally {
if (tableResult != null) {
try {
tableResult.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}Novice guide
Logger
The log that comes with Java.
Common usage is as follows, print log information:
logger.error("Database connection failed",e)
logger.info("tableName:{},comment: {}", tableName, comment), {} is the placeholder
LoggerFactory.getLogger(xxx.class)
Specify the class to initialize the log object. When outputting the log, you can print it out The category of the log information.
Connection
The Connection object is used to open a connection to a data source.
Class.forName(driverName)
Load the driver.
DriverManager.getConnection(url,user,password)
Get the database connection.
PreparedStatement
One of the APIs used to execute SQL query statements.
ResultSet
The result set (ResultSet) is an object returned by the query results in the data. The result set is an object that stores the query results.
ps = conn.prepareStatement(SQL_SHOW_TABLE_STATUS)
The SQL statement will be precompiled before execution, and then the SQL statement will be executed and the result will be returned.
tableResult = ps.executeQuery()
Store the query results of the database response in the ResultSet class object for our use.
next() method in ResultSet
The initial position of the pointer in ResultSet is before the first line; calling the next() method for the first time will set the first line to the current line.
name and comment
respectively represent the table name and table comment queried after executing the database show table status statement. Use the getString() method of ResultSet to get the corresponding value.
The above is the detailed content of How does Java code read a database table?. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version







