常规JDBC设置 [英] General JDBC Setup

查看:70
本文介绍了常规JDBC设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Debian服务器上建立了一个MySQL数据库,并且可以从phpMyAdmin客户端正常工作.我目前正在开发一个项目,以编写一个Java服务器,该服务器将能够通过JDBC连接使用该服务器上已经存在的MySQL数据库.我看过许多教程和文档,但它们似乎都只是在解释如何执行客户端代码,但是我还没有弄清楚如何成功地打开与服务器的JDBC连接.就我而言,我相信程序已经正确设置了驱动程序,因为它不再崩溃(我只是将程序的Java Build Path定向到MySQL提供的Connector/J).就我的程序而言,这就是它的样子……

So I have a MySQL database set up on a Debian server and it works fine from a phpMyAdmin client. I'm currently working on a project to write a Java server that would be able to use the MySQL database that is already on this server through a JDBC connection. I've looked at many tutorials and documentations but all of them seem to just explain how to do client-side code, but I have yet to figure out how to even successfully open a JDBC connection to the server. As far as I am concerned, I believe that program has the drivers properly set up because it's not crashing anymore (I simply direct the Java Build Path of my program to the Connector/J provided by MySQL). As far as my program goes, this is what it looks like...

import java.sql.*;

public class JDBCTest {
    public static void main(String[] args) {
        System.out.println("Started!");
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            System.out.println("Driver registered. Connecting...");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "password");
            System.out.println("Connected!");
            conn.close();
        } catch (SQLException e) {
            System.out.println("Error!");
            e.printStackTrace();
        }
    }
}

这是打印出来的...

This is what's printed...

Started!
Driver registered. Connecting...

就好像DriverManager.getConnection(String)只是冻结在那里一样.我确定这是服务器的问题,因为当我故意拼写localhost或IP地址时,程序会在20秒内崩溃.这只是永远挂在那里.

It's as if the DriverManager.getConnection(String) just freezes there. I'm sure this is a problem with the server because when I intentionally misspell localhost, or an IP address, the program crashes within 20 seconds. This just hangs there forever.

很抱歉,我的最后一个问题是,是否有人可以提供我应该做什么或应该在服务器上安装的任何信息,以使其正常工作?非常感谢!

Sorry about this wall of text, but my final question is if anyone has any information what I should do or install on the server to get this to work? Thank you so much!

推荐答案

尝试以下操作:

public class MySqlDemo {

public static void main(String [] args) {

        java.sql.Connection conn = null;

        System.out.println("SQL Test");

        try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                conn = java.sql.DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test?user=root&password=");

        }
        catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                }

        System.out.println("Connection established");
}

这篇关于常规JDBC设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆