如何使用 java 连接到 mariadb?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37909487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I connect to mariadb using java?
提问by ALEENA JOHN
I've used:
我用过:
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "username", "password"
);
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE a (id int not null primary key, value varchar(20))");
stmt.close();
connection.close();
but it gives an error "No route to host"
但它给出了一个错误“没有到主机的路由”
采纳答案by ALEENA JOHN
Java MariaDB example:
Java MariaDB 示例:
//STEP 1. Import required packages
package mariadb;
import java.sql.*;
public class Mariadb {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
static final String DB_URL = "jdbc:mariadb://192.168.100.174/db";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args) {
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://192.168.100.174/db", "root", "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
I use this example.I change the bind address to 127.10.230.440. and i restart the server sudo /etc/init.d/mysql start .
我使用这个例子。我将绑定地址更改为 127.10.230.440。我重新启动服务器 sudo /etc/init.d/mysql start 。
List item
项目清单
回答by Dungeon_master
I think the problem is with TCP/IP port . Mariadb is not listening to the local host.
You should try : Configure mariadb to listen on localhost.
In the /etc/my.cnf
config file, under the [mysqld] line, add the following:
我认为问题出在 TCP/IP 端口上。Mariadb 没有监听本地主机。您应该尝试:配置 mariadb 以侦听本地主机。在/etc/my.cnf
配置文件的 [mysqld] 行下,添加以下内容:
bind-address = 127.10.230.440
bind-address = 127.10.230.440
Or try to disconnect the MYSQL database first.
或者先尝试断开MYSQL数据库。
回答by Gaurav
public class ConnectionDemo {
String userName,password,url,driver;
Connection con;
Statement st;
public ConnectionDemo() {
userName="root";
password="root";
url="jdbc:mariadb://localhost:3306/stud";
driver="org.mariadb.jdbc.Driver";
try {
Class.forName(driver);
con=DriverManager.getConnection(url, userName, password);
st=con.createStatement();
System.out.println("Connection is successful");
} catch (Exception e) {
e.printStackTrace();
}
}