2016-09-26 32 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 - 访问

相关问题