我有成功用docker和dbeaver連到mssql。
但後來改成servlet就不行了
這是我的connection factory
package utility;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final Connection connection = createConnection();
public static Connection getConnection() {
return connection;
}
public static Connection createConnection() {
String urlStr = "jdbc:sqlserver://localhost:1433;databaseName=JDBCDemoDB;user=sa;password=Passw0rd";
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(urlStr);
return conn;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
public static void closeConnection() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
謝謝
看起來像是「connection 建立失敗」
可以試試看寫隻簡單的 java 程式測測看是否能成功建立 connection
大概像這樣
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connect = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=JDBCDemoDB;user=sa;password=Passw0rd");
statement = connect.createStatement();
resultSet = statement.executeQuery("SELECT * FROM dbname.tablename");
如果不用servlet和jsp是可以正常連到Dbeaver
Dbeaver 是 Dbeaver
現在是你的 servlet/jsp 連不上 SQL Server
我的建議是可以試試看寫隻簡單的 java 程式測測看是否能成功建立 connection
如果不願意試
就當我沒說
剛剛試了,沒有成功建立connection
e.printStackTrace 和 e.getMessage 內容如下:
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at utility.ConnectionFactory.test(ConnectionFactory.java:27)
at servlet.index.doGet(index.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:831)
com.microsoft.sqlserver.jdbc.SQLServerDriver
謝謝
1.錯誤訊息的「最前面」沒看到(eg.No suitable driver found...)
2.我的建議是「純 .java 程式」而不是「servlet」
不管以上兩點
猜測可能是你沒有安裝/下載jdbc driver
可以試試看
這裡我不太明白. 我對我這邊的情況的理解是: 因為我的是mac, 不可以直接用jdbc連到mssql. 網上的教學是要用docker和dbeaver.
而在docker和dbeaver的情況下我的jdbc是可以操作到在dbeaver上的mssql.
所以你的意思是要用jdbc, 不用docker和dbeaver, 直接連到mssql嗎?
這樣算是有安裝到jdbc driver嗎?
謝謝~
1.jdbc 是 java 程式連接至資料庫的統一介面,跟 windows/mac 無關
2.Dbeaver 連到 MSSQL 成功,表示 DBeaver 抓得到 jdbc driver
3.Servlet/JSP 無法建立 connection,表示 Servlet/JSP 抓不到 jdbc driver