我覺得上面的答案是不循環會,並有一些缺乏細節。這段代碼可以改進,只顯示列名和相應的數據類型。這裏是完整的工作代碼
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Test {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String HOST = "192.168.56.101";
private static final String PORT = "3316";
private static final String CONNECTION_URL = "jdbc:mysql://"+HOST+":"+PORT+"/";
private static final String USERNAME = "user";
private static final String PASSWORD = "pwd";
private static final String DATABASE = "db";
private static final String TABLE = "table";
private static final String QUERY = "select * from "+DATABASE+"."+TABLE+" where 1=0";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(DRIVER);
Connection con = DriverManager.getConnection (CONNECTION_URL , USERNAME, PASSWORD);
ResultSet rs = con.createStatement().executeQuery(QUERY);
if (rs != null) {
System.out.println("Column Type\t\t Column Name");
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getColumnTypeName(i)+"\t\t\t"+rsmd.getColumnName(i));
}
}
}
}
大家好。這是未來訪客的信息。如果您的要求是獲取字段值,而不考慮該列的數據類型,則可以在插入時使用resultSet的getObject方法和PreparedStatement的setObject方法。這是一個例如。 '而(rsdata.next()) \t \t { \t \t \t對(INT I = 0; I
learner