无法连接到 Mariadb Xampp 数据库
Posted
技术标签:
【中文标题】无法连接到 Mariadb Xampp 数据库【英文标题】:Cannot connect to Mariadb Xampp-Database 【发布时间】:2020-04-21 09:49:55 【问题描述】:我正在尝试通过 eclipse 和 tomcat 连接到本地 MariaDB 数据库。当我运行程序时,我收到以下消息:
java.sql.SQLNonTransientConnectionException: Could not connect to localhost:8080 : unexpected end of stream, read 0 bytes from 4 (socket was closed by server)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:240)
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:171)
at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1132)
at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:561)
at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:175)
at org.mariadb.jdbc.Driver.connect(Driver.java:92)
这是我正在执行的代码:
public void verbinde()
Connection conn = null;
Statement stmt = null;
try
//STEP 2: Register JDBC driver
Class.forName("org.mariadb.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:8080/test?user=root&password=root");
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE REGISTRATION "
+ "(id INTEGER not NULL, "
+ " first VARCHAR(255), "
+ " last VARCHAR(255), "
+ " age INTEGER, "
+ " PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
catch (SQLException se)
//Handle errors for JDBC
se.printStackTrace();
catch (Exception e)
//Handle errors for Class.forName
e.printStackTrace();
finally
//finally block used to close resources
try
if (stmt != null)
conn.close();
catch (SQLException se)
// do nothing
try
if (conn != null)
conn.close();
catch (SQLException se)
se.printStackTrace();
//end finally try
//end try
System.out.println("Goodbye!");
//end main
//end JDBCExample
【问题讨论】:
【参考方案1】:MariaDB 的默认端口是 3306 而不是 8080
jdbc:mariadb://localhost:3306/test?user=root&password=root
8080 是你的 tomcat 运行的端口
顺便说一句:使用 try-with-resources 而不是 try-catch-finally 块
【讨论】:
嘿,仍然没有连接。这是 phpmyadmin localhost:8080/phpmyadmin/db_structure.php?server=1&db=test 的 URL。 尝试使用 3306 但也没有连接。 Tomcat 的 HTTP/1.1 端口应该是什么?以上是关于无法连接到 Mariadb Xampp 数据库的主要内容,如果未能解决你的问题,请参考以下文章
Dreamweaver CC无法通过PHP连接到我的本地XAMPP测试服务器
Android设备无法连接到本地主机中的mysql [关闭]
将 Laravel 连接到 XAMPP MySQL (MariaDB) 数据库 SQLSTATE[HY000] [2006] MySQL 服务器已消失