2016-09-26 28 views

回答

1

在Java中訪問關係數據庫的標準方法是使用JDBC。有很多教程。

爲爲例(對於一個MySQL DB):

public Connection getConnection() throws ClassNotFoundException, SQLException { 
     Class.forName("com.mysql.jdbc.Driver"); 
     // could be an IP address, including localhost instead of an URL 
     return DriverManager.getConnection("jdbc:mysql://my-url.com:3306/my_database", "user", "password"); 
    } 

    public void test() throws ClassNotFoundException, SQLException { 
     try(Connection c = getConnection()) { 
      try (PreparedStatement ps = c.prepareStatement("SELECT id, name FROM person WHERE email = ?")) { 
       ps.setString(1, "[email protected]"); 
       try (ResultSet rs = ps.executeQuery()) { 
        while (rs.next()) { 
         Integer id = rs.getInt("id"); 
         String name = rs.getString("name"); 
         System.out.println(id + " " + name); 
        } 
       } 
      } 
     } 
    } 

駕駛員的罐子必須是在類路徑


編輯

UcanAccess是JDBC驅動的MS - 訪問

相關問題