0
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;
}

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

请有人可以帮助我..谢谢

4

3 回答 3

1

从可用的堆栈跟踪和代码片段看来,您dbPortchatserver,而 port 是 int 之类的3306,因此在将其解析为 Integer 时它失败并给了您NumberFormatException

所以而不是

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

试试这个

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

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

DriverManager.getConnection("jdbc:mysql://localhost:3306/chatserver",
                            "root", "root");

在上面您可以放置​​您的用户名、密码、dbPort、数据库名称等

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

于 2012-06-08T08:29:05.403 回答
0

我认为问题出在

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

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

于 2012-06-08T08:30:19.897 回答
0

看看我下面的代码,并尝试在此处实现它....我希望它会运行顺利..一旦您开始获得所需的查询结果,请将其修改为您的心脏内容以进行进一步的研发。

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);


    }
于 2012-06-08T09:12:52.793 回答