Em không hiểu vì sao mà mãi em không connect được với sql server có ai đó cho em biết nguyên nhân vì sao không ạ.
package connect;
import dao.PersonalDAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBConnect {
//kết nối đến MYSQL
public static Connection getConnectionMYSQL()
{
final String url="jdbc:mysql:/localhost:3306/payroll";
final String user="root";
final String password="123456";
try{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
//kết nối đến MS SQL Server
public static Connection getConnectionSQLServer()
{
Connection conn = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://2DFPCVKWUZTOSY4\\SQLEXPRESS:49794;databaseName=HR","sa","123456");
} catch (Exception e)
{
System.out.println("Loi roi do " + e.getMessage() );
}
return conn;
}
public static void main(String[] args) {
Connection c = getConnectionMYSQL();
if (c == null) {
System.out.println("something wrong : Sai roi do ");
} else {
System.out.println("ok");
}
}
}