Java通过JDBC远程连接mySQL:ODBC桥

发布于 2024-09-07 05:35:11 字数 1153 浏览 0 评论 0原文

我在使用 Java 远程连接到 mySQL 数据库时遇到问题。这是我的错误消息:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] 未找到数据源名称并且未指定默认驱动程序

我确定我的 IP 地址和地址我正在使用工作端口,因为我使用相同的 ip & mySQL 客户端程序的端口。

我的托管公司不支持 JDBC,因此我使用 JDBC-ODBC 桥接器。

这是我的课程:

public class SQLdataBase {

private Connection con;
private Statement st;
private static final String url="jdbc:odbc://xxx.xxx.xxx.xxx:3306";
private static final String className="sun.jdbc.odbc.JdbcOdbcDriver";

    private static  String user;
private static  String pass;

 SQLdataBase(String userName, String password) {
           user=userName;
        pass=password;
    try {
            Class.forName(className);
 con = DriverManager.getConnection(url, user, pass);
 System.out.println("success");
            st = con.createStatement();
        } catch (Exception ex) {
            System.out.println(ex);
        }
  //do whatever database processing is required
 }

    public void queryNoReturn(String query) throws SQLException{
        st.executeQuery(query);
    }

 }

错误发生在这一行: con = DriverManager.getConnection(url, 用户, pass);

我做错了什么?

I am having problems remotely connecting to my mySQL database in Java. Here is my error message:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

I am sure my ip address & port I am using work, since I am using the same ip & port for a mySQL client program.

My hosting company do not support JDBC so I am using a JDBC-ODBC bridge.

Here is my class:

public class SQLdataBase {

private Connection con;
private Statement st;
private static final String url="jdbc:odbc://xxx.xxx.xxx.xxx:3306";
private static final String className="sun.jdbc.odbc.JdbcOdbcDriver";

    private static  String user;
private static  String pass;

 SQLdataBase(String userName, String password) {
           user=userName;
        pass=password;
    try {
            Class.forName(className);
 con = DriverManager.getConnection(url, user, pass);
 System.out.println("success");
            st = con.createStatement();
        } catch (Exception ex) {
            System.out.println(ex);
        }
  //do whatever database processing is required
 }

    public void queryNoReturn(String query) throws SQLException{
        st.executeQuery(query);
    }

 }

The error occures at this line:
con = DriverManager.getConnection(url, user, pass);

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-09-14 05:35:11
String url="jdbc:odbc://xxx.xxx.xxx.xxx:3306";

在 ODBC 中,通常在 URL 中使用数据源名称 (DSN),而不是主机名:端口。如果托管文档中对此不清楚和/或无法直接透露,那么您需要联系他们以获取确切的 DSN。一旦知道,请使用以下 URL:

 String url="jdbc:odbc:dataSourceName";
String url="jdbc:odbc://xxx.xxx.xxx.xxx:3306";

In ODBC, you normally use the data source name (DSN) instead of hostname:port in URL. If this is unclear and/or not directly revealable in the documentation of the hosting, then you'll need to contact them for the exact DSN. Once known, then use the following URL:

 String url="jdbc:odbc:dataSourceName";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文