Lỗi khi kết nối ms SQL vào java sử dụng eclipse
Bạn đã cài tomcat hay add mysql jdbc vào trong Libraries chưa?! Phải có 2 cái đó, kèm theo tên tk + pass giống lúc bạn set up cho localhost nhé
3 Likes
Nếu là mysql thì cần phải có gói mysql-connector-java.jar hỗ trợ kết nối. Trong mysql nó có 1 schema là world, bạn thử test xem
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// getConnection(String url, String user, String password)
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM countrylanguage;");
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
}
connect.close();
} catch (Exception e) {
System.out.println(e);
}
3 Likes

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?