如何使用java连接到mariadb?

k4aesqcs  于 2023-10-20  发布在  Java
关注(0)|答案(4)|浏览(173)

我用过:

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();

但它给出了一个错误“没有路由到主机”

kninwzqo

kninwzqo1#

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

我使用这个例子。我将绑定地址更改为127.10.230.440。我重启服务器sudo /etc/init.d/mysql start。
列表项

c7rzv4ha

c7rzv4ha2#

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

    }
jfgube3f

jfgube3f3#

我认为问题出在TCP/IP端口上。Mariadb不听本地主机。你应该尝试:配置mariadb监听localhost。在/etc/my.cnf配置文件中,在[mysqld]行下添加以下内容:
bind-address = 127.10.230.440
或者尝试先断开MYSQL数据库连接。

xu3bshqb

xu3bshqb4#

我遇到了同样的问题,原因是我有错误的司机。你需要一个mariadb-java-client-XXX.jar(没有源版本!)并将其添加到类路径中。“我知道,我知道,我知道。”

相关问题