继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

通过表字段生产类属性

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
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 + ";");
        }
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP