gpt4 book ai didi

java - JDBC - 无法与本地主机连接

转载 作者:行者123 更新时间:2023-12-02 04:39:31 24 4
gpt4 key购买 nike

我正在尝试连接名为 SimpleBase 的 Microsoft SQL Server 数据库,该数据库位于我的计算机上,代码如下:

    static final String JDBCDriver = "com.mysql.jdbc.Driver";
static final String dbURL = "jdbc:mysql://localhost/SimpleBase";
public static void main(String[] args) throws ClassNotFoundException, SQLException {

Connection conn = null;
Statement stmt = null;

try{
Class.forName(JDBCDriver);
System.out.println("Connecting");
conn = DriverManager.getConnection(dbURL);
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}

Eclipse 中的输出是这样的:

Connecting

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at PROKOM.Zadanie11.test.main(test.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

我怀疑我做错了什么。有人能告诉我什么吗?如果这不完全是我的错,有人能告诉我出了什么问题吗?

最佳答案

MySQLSQL Server 使用不同的驱动程序类和连接 URL

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    
connection = DriverManager.getConnection
("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SimpleBase", "user", "");

SQL Server 使用JDBC type 4 driver因此无需显式加载 JDBC 驱动程序

关于java - JDBC - 无法与本地主机连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com