0
我正在使用hsqldb進行單元測試(非生產)。這些測試需要訪問表的主鍵,但我似乎無法使其工作。順便說一下,我正在使用最新版本2.0.0在HSQLDB中獲取PK難
我已經創建了一個小片段來重新創建問題。任何反饋將不勝感激
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws Exception {
Connection conn = getHSQLConnection();
System.out.println("Got Connection.");
Statement st = conn.createStatement();
st.executeUpdate("create table survey (id int,name varchar (20), PRIMARY KEY (id));");
ResultSet rs = null;
DatabaseMetaData meta = conn.getMetaData();
rs = meta.getPrimaryKeys(null, null, "survey");
String pk = null;
if (rs.next()) {
pk = rs.getString("COLUMN_NAME");
System.out.println("getPrimaryKeys(): columnName=" + pk);
}
else {
System.out.println("Couldn't get the PK");
}
st.close();
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/test1";
return DriverManager.getConnection(url, "sa", "");
}
}