Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testmysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- /**
- *
- * @author Thieu Mao
- */
- public class TestMySQL {
- private Connection conn;
- public void testConnectMySQL() {
- String db = "TestMySQL"; //Tên database
- String user = "root"; //Mặc định là root
- String pass = "admin"; //Mật khẩu quản trị MySQL
- try {
- String url = "jdbc:mysql://localhost/" + db + "?user=" + user + "&password=" + pass;
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url);
- System.out.println("Kết nối thành công.");
- } catch (Exception e) {
- System.out.println("Lỗi: " + e.getMessage());
- }
- }
- public static void main(String[] args) {
- TestMySQL tm = new TestMySQL();
- tm.testConnectMySQL();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment