如何在制作jdbc示例时删除此异常?

时间:2014-01-26 05:52:54

标签: java jdbc

我正在创建一个简单的jdbc示例。我收到此错误。我在我的mac机器上运行我的sql server。请告诉我如何与我的sql建立连接并删除此错误。我已经包含了连接器jar档案。

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:358)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2489)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at HelloWorldExample.main(HelloWorldExample.java:17)
Caused by: java.net.UnknownHostException: local
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:877)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1230)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1181)
    at java.net.InetAddress.getAllByName(InetAddress.java:1111)
    at java.net.InetAddress.getAllByName(InetAddress.java:1047)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:248)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:308)
    ... 15 more

这是我的代码..我正在使用mac

import java.sql.*;
/**
* HelloWorldExample : JDBC Tutorial that creates a simple database, stores in a single row
* and retrieves the data to then be displayed on standard out. The database is cleaned up 
* to reduce clutter for this example.
* Author: Kevin Hooks
* Date: April 18th, 2012
**/
public class HelloWorldExample {



    public static void main(String[] args) throws SQLException{
        // Note that you can use TCP/IP by using this URL
        // "jdbc:raima:rdm://localhost"
        // The rdmsqlserver must be running to use TCP/IP
        Connection Conn = DriverManager.getConnection("jdbc:mysql://local");
        try {

            Statement Stmt =  Conn.createStatement();
            try {
                try { //Since the database is created here, it cannot be created twice
                    Stmt.execute("DROP DATABASE hello_db");
                } catch (SQLException exception) {} 

                Stmt.execute("CREATE DATABASE hello_Db");
                Stmt.execute("CREATE TABLE hello_table (f00 char(31))");
                Conn.commit();

                PreparedStatement PrepStmt = Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");
                try { //Sets parameter value to a string         
                    PrepStmt.setString(1, "Hello World!");
                    PrepStmt.execute();
                    Conn.commit();

                    ResultSet RS = Stmt.executeQuery("SELECT * FROM hello_table");
                    try {

                        while(RS.next() != false) {
                            System.out.println(RS.getString(1));
                        }
                    } finally {
                        RS.close();
                    }
                } finally { //Cleans up by dropping database in this case
                    Stmt.execute("DROP DATABASE hello_db");
                    PrepStmt.close();
                }
            } finally {
                Stmt.close();
            }

        } catch (SQLException exception) {
            System.err.println("SQLException: " + exception.toString());

        }finally {
             Conn.close();           
        }

    }

}

4 个答案:

答案 0 :(得分:3)

环回地址的主机名是localhost,而不是local

答案 1 :(得分:1)

您已声明错误的连接字符串。请根据以下规则对其进行修改

Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name");

答案 2 :(得分:1)

  

线程“main”中的异常com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败   

  成功发送到服务器的最后一个数据包是0毫秒前。驱动程序未收到来自服务器的任何数据包。

     

在HelloWorldExample.main(HelloWorldExample.java:17)

第17行:

Connection Conn = DriverManager.getConnection("jdbc:mysql://local");

您的程序无法建立与数据库的连接,请确保您的连接URL正确无误。

local应替换为localhost127.0.0.1

您还应该注意,您实际上从未选择要使用的数据库。

答案 3 :(得分:0)

您的网址应为localhost,然后添加数据库名称

看看这个

  import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.SQLException;

  public class JDBCExample {

  public static void main(String[] argv) {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager
        .getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {


    System.out.println("Failed to make connection!");
    }
  }
}