简体   繁体   English

JDBC MYSQL连接问题

[英]JDBC MYSQL connection issue

I am finally able to create a simple java program through console and notepad which connects to MYSQL database. 我终于可以通过控制台和记事本创建一个简单的Java程序,该程序连接到MYSQL数据库。 But when i access the database using 但是当我使用访问数据库时

conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();

my code simply keeps running for 1 or 2 minutes outputting a lot of information (I noticed some kind of trace) before it stops executing. 在停止执行之前,我的代码只运行了1或2分钟,就输出了很多信息(我注意到了某种跟踪)。

Here is my code: 这是我的代码:

public class Database1{

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/emp?connectTimeout=3000";

    static final String USER = "root";
    static final String PASS = "admin";

    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs=null;

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch(Exception e){

            System.out.println("Unable to connect!!");
        }

        System.out.println("Connecting to database...");

        try{

            conn = DriverManager.getConnection(DB_URL,USER,PASS);
        } catch(Exception e){
            System.out.println("Unable to access Database!!");
        }

        System.out.println("Creating statement...");

        try{
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Employees");
        }catch(Exception e){
            System.out.println("Unable to access Database!!");
        }
        System.out.println("ID             NAME         ");
    }
}

Below is the output that i get(and i could copy only the details at the end of screen): 以下是我得到的输出(我只能在屏幕末尾复制详细信息):

          ..............ql.jdbc.ByteArrayBuffer.getCapacity(..) returning 16384
Thu Jun 05 12:28:53 IST 2014 TRACE:       <--  MysqlIO.java:2357 com.mysql.jdbc.
MysqlIO.reclaimLargeReusablePacket(..) returning null
Thu Jun 05 12:28:53 IST 2014 TRACE:       --> ResultSet.java:6643 com.mysql.jdbc
.ResultSet.toString()
Thu Jun 05 12:28:53 IST 2014 TRACE:       <--  ResultSet.java:6643 com.mysql.jdb
c.ResultSet.toString(..) returning "com.mysql.jdbc.ResultSet@546ccad7"
Thu Jun 05 12:28:53 IST 2014 TRACE:      <--  MysqlIO.java:1411 com.mysql.jdbc.M
ysqlIO.readAllResults(..) returning com.mysql.jdbc.ResultSet@546ccad7
Thu Jun 05 12:28:53 IST 2014 TRACE:      --> ResultSet.java:6643 com.mysql.jdbc.
ResultSet.toString()
Thu Jun 05 12:28:53 IST 2014 TRACE:      <--  ResultSet.java:6643 com.mysql.jdbc
.ResultSet.toString(..) returning "com.mysql.jdbc.ResultSet@546ccad7"
Thu Jun 05 12:28:53 IST 2014 TRACE:     <--  MysqlIO.java:1634 com.mysql.jdbc.My
sqlIO.sqlQueryDirect(..) returning com.mysql.jdbc.ResultSet@546ccad7
Thu Jun 05 12:28:53 IST 2014 TRACE:     --> ConnectionProperties.java:1901 com.m
ysql.jdbc.ConnectionProperties.getMaintainTimeStats()
Thu Jun 05 12:28:53 IST 2014 TRACE:     <--  ConnectionProperties.java:1901 com.
mysql.jdbc.ConnectionProperties.getMaintainTimeStats(..) returning true
Thu Jun 05 12:28:53 IST 2014 TRACE:     --> ConnectionProperties.java:1809 com.m
ysql.jdbc.ConnectionProperties.getGatherPerformanceMetrics()
Thu Jun 05 12:28:53 IST 2014 TRACE:      --> ConnectionProperties.java:83 com.my
sql.jdbc.ConnectionProperties$BooleanConnectionProperty.getValueAsBoolean()
Thu Jun 05 12:28:53 IST 2014 TRACE:      <--  ConnectionProperties.java:83 com.m
ysql.jdbc.ConnectionProperties$BooleanConnectionProperty.getValueAsBoolean(..) r
eturning false
Thu Jun 05 12:28:53 IST 2014 TRACE:     <--  ConnectionProperties.java:1809 com.
mysql.jdbc.ConnectionProperties.getGatherPerformanceMetrics(..) returning false
Thu Jun 05 12:28:53 IST 2014 TRACE:     --> ResultSet.java:6643 com.mysql.jdbc.R
esultSet.toString()
Thu Jun 05 12:28:53 IST 2014 TRACE:     <--  ResultSet.java:6643 com.mysql.jdbc.
ResultSet.toString(..) returning "com.mysql.jdbc.ResultSet@546ccad7"
Thu Jun 05 12:28:53 IST 2014 TRACE:    <--  Connection.java:2964 com.mysql.jdbc.
Connection.execSQL(..) returning com.mysql.jdbc.ResultSet@546ccad7
Thu Jun 05 12:28:53 IST 2014 TRACE:    --> ResultSet.java:6643 com.mysql.jdbc.Re
sultSet.toString()
Thu Jun 05 12:28:53 IST 2014 TRACE:    <--  ResultSet.java:6643 com.mysql.jdbc.R
esultSet.toString(..) returning "com.mysql.jdbc.ResultSet@546ccad7"
Thu Jun 05 12:28:53 IST 2014 TRACE:   <--  Connection.java:2949 com.mysql.jdbc.C
onnection.execSQL(..) returning com.mysql.jdbc.ResultSet@546ccad7
Thu Jun 05 12:28:53 IST 2014 TRACE:   --> ResultSet.java:5634 com.mysql.jdbc.Res
ultSet.getUpdateID()
Thu Jun 05 12:28:53 IST 2014 TRACE:   <--  ResultSet.java:5634 com.mysql.jdbc.Re
sultSet.getUpdateID(..) returning -1
Thu Jun 05 12:28:53 IST 2014 TRACE:   --> ConnectionProperties.java:1638 com.mys
ql.jdbc.ConnectionProperties.getCacheResultSetMetadata()
Thu Jun 05 12:28:53 IST 2014 TRACE:   <--  ConnectionProperties.java:1638 com.my
sql.jdbc.ConnectionProperties.getCacheResultSetMetadata(..) returning false
Thu Jun 05 12:28:53 IST 2014 TRACE:   --> ResultSet.java:6643 com.mysql.jdbc.Res
ultSet.toString()
Thu Jun 05 12:28:53 IST 2014 TRACE:   <--  ResultSet.java:6643 com.mysql.jdbc.Re
sultSet.toString(..) returning "com.mysql.jdbc.ResultSet@546ccad7"
Thu Jun 05 12:28:53 IST 2014 TRACE:  <--  Statement.java:868 com.mysql.jdbc.Stat
ement.executeQuery(..) returning com.mysql.jdbc.ResultSet@546ccad7
ID             NAME

C:\JavaP>   

Can anyone here help me to understand what the output is all about and how do i control it please... 有人可以在这里帮助我了解输出的全部内容以及如何控制输出...

Your logs 您的日志

As I can see in your code in the Dropbox, you are using a PostgreSQL database - not a MySQL database a mentioned in your question! 正如我在Dropbox的代码中看到的那样,您正在使用PostgreSQL数据库-问题中没有提到MySQL数据库! That makes a difference. 这有所作为。

Take a look at http://jdbc.postgresql.org/development/privateapi/org/postgresql/Driver.html 看看http://jdbc.postgresql.org/development/privateapi/org/postgresql/Driver.html

This is the documentation of the driver you use. 这是您使用的驱动程序的文档。 This driver has a logger and a certain log level (in your case trace ). 该驱动程序具有记录器和特定的日志级别(在您的情况下为trace )。 You can set the log level or even the logger. 您可以设置日志级别甚至记录器。

org.postgresql.Driver.setLogLevel(java.util.logging.Level.INFO);

Some general things 一些一般的事情

Your code and output looks fine. 您的代码和输出看起来不错。 Well, you do not close your connection and statements, but beside of that, it seems to work. 好吧,您没有关闭连接和语句,但是除此之外,它似乎还可以工作。

You make a connection, you create a statement and as fare as I can see there was no exception. 建立联系,创建一条语句,据我所知,票价也不例外。 In the end you make a query, but you do nothing with the result. 最后,您进行查询, 但对结果不执行任何操作。 That is the reason why you do not see anything. 这就是为什么您什么都看不到的原因。 Then your program ends and stops (because the code finished). 然后您的程序结束并停止(因为代码已完成)。

However, I recommend to change your code a little bit. 但是,我建议稍微更改一下代码。 You simple log away all possible exception 您只需注销所有可能的异常

catch(Exception e){
    System.out.println("Unable to access Database!!");
}

If you do this you will never know what went wrong. 如果这样做,您将永远不会知道出了什么问题。 Instead, print the exception, it is the only piece of information which could tell you what happens: 而是打印异常,它是唯一可以告诉您发生了什么的信息:

catch(Exception e){
    System.out.println("Unable to access Database!!");
    e.printStackTrace();
}

Now you should only think about, if your program could continue if such an exception occurs. 现在,您只应考虑是否可以在发生此类异常的情况下继续执行程序。 Eg if you cannot connect to your database, it is useless to continue. 例如,如果您无法连接到数据库,则无法继续进行。 It will not work! 不起作用! In this case, it is better to leave (or handle the exception): 在这种情况下,最好离开(或处理异常):

catch(Exception e){
    System.out.println("Unable to access Database!!");
    e.printStackTrace();
    return;
}

And last but not least, you need to use the result of your query to see something: 最后但并非最不重要的一点是,您需要使用查询结果来查看内容:

rs = stmt.executeQuery("SELECT * FROM Employees");
while (rs.next()) {
    // get your stuff
}
public class DBConnect {

Statement st;
ResultSet rs;

public DBConnect() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_name", "root", "root");
        st = con.createStatement();
    } catch (Exception e) {
    }
}

public ResultSet readData(String s) {
    try {
        System.out.println("SQL : : : " + s);
        rs = st.executeQuery(s);
    } catch (Exception e) {
    }
    return rs;
}

public int putData(String s) {
    int i = 0;
    try {
        System.out.println("SQL : : : " + s);
        i = st.executeUpdate(s);
    } catch (Exception e) {}
    return i;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM