为了账号安全,请及时绑定邮箱和手机立即绑定

通过表字段生产类属性

标签:
Java
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.sql.Statement;
import org.junit.Test;
public class JdbcExample {
    private Connection getConnection() throws SQLException, ClassNotFoundException {
        String url = "jdbc:mysql:///jt_sys";
        String user = "root";
        String password = "root";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, user, password);
        return connection;
    }
    private void close(ResultSet rs, Statement stmt, Connection connection) throws SQLException {
        if (rs != null && !rs.isClosed()) {
            rs.close();
        }
        if (stmt != null && !stmt.isClosed()) {
            stmt.close();
        }
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    }
    @Test
    public void autoCode() throws ClassNotFoundException, SQLException {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("select * from sys_roles");
        ResultSet resultSet = preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int count = metaData.getColumnCount();
        String name = null;
        String type = null;
        for (int i = 0; i < count; i++) {
            type = metaData.getColumnTypeName(i + 1);
            name = metaData.getColumnName(i + 1);
            if ("BIGINT".equals(type)) {
                type = "Long";
            } else if ("VARCHAR".equals(type)) {
                type = "String";
            } else if ("DATETIME".equals(type)) {
                type = "Date";
            }
            System.out.println("private " + type + " " + name + ";");
        }
    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消