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,阅读手记