JDBC连接数据库

2018-01-15  本文已影响0人  jsjack_wang
1.添加maven依赖
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql.connector.version}</version>
</dependency>
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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读