JDBC连接数据库

1.添加maven依赖

  mysql
  mysql-connector-java
  ${mysql.connector.version}

2.db.properties(resource)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yudada
jdbc.username=root
jdbc.password=123456
3.小栗子
public static void main(String[] args) {
    ResourceBundle rb = ResourceBundle.getBundle("db");
    String driver = rb.getString("jdbc.driver");
    String url = rb.getString("jdbc.url");
    String username = rb.getString("jdbc.username");
    String password = rb.getString("jdbc.password");
    try{
        Class.forName(driver);
        Connection connect = DriverManager.getConnection(url, username, password);
        Statement statement = connect.createStatement();
        String sql = "select id, link from info";
        ResultSet rs = statement.executeQuery(sql);
        while(rs.next()){
            String id = rs.getString("id");
            String link = rs.getString("link");
            System.out.println("id:" + id + " link:" + link);
        }
        rs.close();
        connect.close();
    }catch(Exception e){
        System.out.println(e);
    }
}

你可能感兴趣的:(JDBC连接数据库)