Kết nối csdl với netbeans

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");
        }
}
}

Phải có error log thì mọi người mới trả lời được chứ :joy:.
Cơ mà hôm qua mình cũng trả lời cho 1 bạn về MySQL. Bạn check thử xem

3 Likes

Đường dẫn SQL bị sai nha bạn:

jdbc:mysql://localhost:3306/localhost:3306/payroll

1 Like
Warning:   The web application [/QuanLyNhanSuVaTienLuong] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   Loading application [QuanLyNhanSuVaTienLuong] at [/QuanLyNhanSuVaTienLuong]
Info:   QuanLyNhanSuVaTienLuong was successfully deployed in 1.138 milliseconds.
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   visiting unvisited references
Info:   Loading application [QuanLyNhanSuVaTienLuong] at [/QuanLyNhanSuVaTienLuong]
Info:   QuanLyNhanSuVaTienLuong was successfully deployed in 545 milliseconds.
Info:   Loi ket noi1 Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
Info:   kết nối 1
Warning:   StandardWrapperValve[XuLyDangNhap]: Servlet.service() for servlet XuLyDangNhap threw exception
java.lang.NullPointerException
	at dao.UserDAO.kiemtradangnhap(UserDAO.java:31)
	at controller.XuLyDangNhap.doGet(XuLyDangNhap.java:32)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
	at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
	at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
	at java.lang.Thread.run(Thread.java:748)


Sau khi em chạy thì ra trình duyệt nó báo như vậy ạ. Mong mọi người giúp em hiểu nguyên nhân là bị gì ko ạ.

# HTTP Status 500 - Internal Server Error

**type**  Exception report

**message** Internal Server Error

**description** The server encountered an internal error that prevented it from fulfilling this request.

**exception**

java.lang.NullPointerException

**note**  The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

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