- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Types;
-
- public class TestMysql {
-
- public static void main(String[] args) throws Exception {
-
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true";
- String user = "root";
- String password = "mysql0774mysql";
-
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, user, password);
- if (!conn.isClosed())
- System.out.println("Succeeded connecting to the Database!");
- else
- System.err.println("connect filed");
-
- Statement statement = conn.createStatement();
-
- getTables(conn);
-
- ResultSet resultSet = statement
- .executeQuery("select * from help_keyword");
-
- ResultSetMetaData metaData = resultSet.getMetaData();
- for (int i = 0; i < metaData.getColumnCount(); i++) {
-
- String columnName = metaData.getColumnName(i + 1);
- int type = metaData.getColumnType(i + 1);
- if (Types.INTEGER == type) {
-
- } else if (Types.VARCHAR == type) {
-
- }
- System.out.print(columnName + "\t");
- }
- System.out.println();
-
- while (resultSet.next()) {
- for (int i = 0; i < metaData.getColumnCount(); i++) {
-
- System.out.print(resultSet.getString(i + 1) + "\t");
- }
- System.out.println();
-
- }
- statement.close();
- conn.close();
-
- }
-
- public static String convertDatabaseCharsetType(String in, String type) {
- String dbUser;
- if (in != null) {
- if (type.equals("oracle")) {
- dbUser = in.toUpperCase();
- } else if (type.equals("postgresql")) {
- dbUser = "public";
- } else if (type.equals("mysql")) {
- dbUser = null;
- } else if (type.equals("mssqlserver")) {
- dbUser = null;
- } else if (type.equals("db2")) {
- dbUser = in.toUpperCase();
- } else {
- dbUser = in;
- }
- } else {
- dbUser = "public";
- }
- return dbUser;
- }
-
- private static void getTables(Connection conn) throws SQLException {
- DatabaseMetaData dbMetData = conn.getMetaData();
-
- ResultSet rs = dbMetData.getTables(null,
- convertDatabaseCharsetType("root", "mysql"), null,
- new String[] { "TABLE", "VIEW" });
-
- while (rs.next()) {
- if (rs.getString(4) != null
- && (rs.getString(4).equalsIgnoreCase("TABLE") || rs
- .getString(4).equalsIgnoreCase("VIEW"))) {
- String tableName = rs.getString(3).toLowerCase();
- System.out.print(tableName + "\t");
-
- ResultSet colRet = dbMetData.getColumns(null, "%", tableName,
- "%");
- while (colRet.next()) {
- String columnName = colRet.getString("COLUMN_NAME");
- String columnType = colRet.getString("TYPE_NAME");
- int datasize = colRet.getInt("COLUMN_SIZE");
- int digits = colRet.getInt("DECIMAL_DIGITS");
- int nullable = colRet.getInt("NULLABLE");
-
-
- }
-
- }
- }
- System.out.println();
-
-
-
-
-
-
-
-
- }
-
- }
posted @
2017-08-31 16:34
人情世故
阅读(
3814)
评论()
编辑
收藏
举报
"不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!"