与mysql服务器的连接失败

时间:2012-06-08 08:25:02

标签: java mysql jdbc database-connection

x1.instance3650.db.xeround.com-nino_db 
[chatserver.sql.MySqlConnection.rehash()]   
this is con null [.()]   
connection to MySQL server failed 
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:  
Cannot load connection class because of underlying exception: 
'java.lang.NumberFormatException: For input string: "chatserver"'.  
[chatserver.sql.MySqlConnection.connect()]  
Exception in thread "AWT-EventQueue-0"  [.()] 
java.lang.NullPointerException [.()]  at   
chatserver.sql.MySqlConnection.ensureConnected(MySqlConnection.java:105) [.()]  at 
chatserver.sql.MySqlConnection.getServerConfig(MySqlConnection.java:936) [.()]  at 
chatserver.objects.ServerConfig.loadConfigFromSql(ServerConfig.java:57) [.()]  at 
chatserver.objects.ServerConfig.<init>(ServerConfig.java:49) [.()]  at 
chatserver.objects.Server.<init>(Server.java:36) [.()]  at 
chatserver.objects.Server.getInstance(Server.java:88) [.()]  at 
chatserver.main.ServerMain$1.run(ServerMain.java:24) [.()]  at 
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) [.()]  at 
java.awt.EventQueue.dispatchEvent(EventQueue.java:597) [.()] at 
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) [.()]  
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) [.()]  
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) [.
()]  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) [.()]  at 
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) [.()]  at 
java.awt.EventDispatchThread.run(EventDispatchThread.java:122) [.()]

这是由以下代码块引发的。

try {
     //conn = DriverManager.getConnection ("jdbc:mysql://localhost:chatserver");
     // Joshua - change
     // dbPassword = "";
     conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":"
                  + dbPort + "/" + dbDatabase + "?" + "user=" + dbUser
                    + "&"
                    + "password=" + dbPassword);
     lastMySqlActionTime = new Date().getTime();
} catch (SQLException e) {
     System.out.println("this is con "+conn);
     ServerLog.logError("connection to MySQL server failed "+e);
     return false;
}

我可以通过命令提示符访问数据库并使用数据库...它不会在驱动程序中抛出任何错误......

PLZ有人可以帮助我......谢谢你

3 个答案:

答案 0 :(得分:1)

从堆栈跟踪和可用的代码片段来看,dbPort似乎是chatserver,而端口是类似3306的int,因此在将其解析为Integer时,它会失败并为您提供{ {1}}。

所以而不是

NumberFormatException

试试这个

conn = DriverManager.getConnection ("jdbc:mysql://localhost:chatserver")

完整的连接字符串如下所示:

conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/chatserver") 

在上面,您可以输入您的用户名,密码,dbPort,databasename e.t.c。

这里我假设您要连接到名为chatserver的模式

答案 1 :(得分:0)

我认为问题在于

conn = DriverManager.getConnection ("jdbc:mysql://localhost:chatserver");

你得到一个NumberFormatException,因为端口号应该是数字。

答案 2 :(得分:0)

请查看下面的代码,并尝试将其实现为此....我希望它能够顺利运行..一旦您开始获得所需的查询结果,请将其修改为您的心脏内容以获得更多R&amp; D.

请在此计划中change the password and username

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Vector;

import javax.swing.JTable;

import com.my.views.*;


public class DBDiary {

    Connection conn;
    EntryDisplay entryD;


    public DBDiary(){

        this.getConn();

    }

    public Connection getConn(){

        try {
            conn = getConnection();

        } catch (SQLException e) {

            System.out.println("Connection Couldnt be Obtained");
        }
           return conn;
    }


    public static Connection getConnection() throws SQLException {

        String drivers = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "root";

        System.setProperty(drivers,"");

        return DriverManager.getConnection(url,username,password);

    }

    public void createTable() {

        Statement stat = null;
        String sql = "create table items(item varchar(30), cost DOUBLE(12,3),day integer,month varchar(15), year integer);";
        if (conn != null){

            try {

                 stat = conn.createStatement();

            } catch (SQLException e) {

                System.out.println("Connection Couldnt be Obtained");
            }

            if (stat != null){

                try {
                    stat.executeUpdate(sql);
                } catch (SQLException e) {
                    //System.out.println("Table already Exists");
                }       
            }

        }
    }


    public void addItems(String item, double cost,int day, String month, int year) {

        PreparedStatement pstat = null;

        String sql = "INSERT INTO ITEMS Values(?,?,?,?,?);";

        if (conn != null){

            try {

                 pstat = conn.prepareStatement(sql);


            } catch (SQLException e) {

                System.out.println("Connection Couldnt be Obtained");
            }

        }

        if (pstat != null) {

            try {

                 pstat.setString(1, item);
                 pstat.setDouble(2, cost);
                 pstat.setInt(3, day);
                 pstat.setString(4, month);
                 pstat.setInt(5,year);
                 pstat.executeUpdate();

            } catch (SQLException e) {

                e.printStackTrace();
                System.out.println("Insertion of the entry was unsuccessful");
            }
        }


    }


    public static void main(String[] args) {

    DBDiary db = new DBDiary();
    db.createTable();
    db.addItems("Cards", 40.00,29, "Mar", 2012);


    }