我正在製作一個程序來將數據存儲在數據庫中。我創建了獲取連接的代碼,以讀取所有可用的數據庫,但現在我希望能夠從可用數據庫中選擇一個特定數據庫。任何人都可以幫助我如何? 下面的dode是與基礎和可用數據庫列表的連接。從列表中選擇一個數據庫
public static void main (String[] args) throws Exception {
try {
System.out.println("get the connection");
}
catch(Exception e)
{
System.out.println("SQLException: " + e.getMessage());
}
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()){
System.out.println (" " +res.getString(1));
}
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Choose a database");
String theDatabase = keyboard.toString();
while (theDatabase)
{
System.out.println("Enter an existing database");
theDatabase = keyboard.toString();
}
System.out.println("You choose "+theDatabase);
}
我該如何繼續?我想用選定的數據庫繼續其餘的程序。 我在寫什麼while(theDatabase)???
如果您發現'DatabaseMetaData.getCatalogs()',你爲什麼不嘗試的邏輯方法['Connection.setCatalog(..)'](http://docs.oracle.com/javase /7/docs/api/java/sql/Connection.html#setCatalog(java.lang.String)) – 2013-04-26 09:57:29
我該怎麼做呢? – dedmar 2013-04-26 10:01:25
如何將目錄名稱(數據庫以MySQL術語)傳遞給'setCatalog(...)' – 2013-04-26 10:42:48