java 连接mysql服务器失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10945372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:11:16  来源:igfitidea点击:

connection to mysql server failed

javamysqljdbcdatabase-connection

提问by nissal

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.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) [.()]

This is thrown by the following block of code..

这是由以下代码块抛出的..

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

I can get access to the databases through command prompt and use database...it is not throwing error any error in driver...

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

plz some one can help me..thank you

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

回答by mprabhat

From the stacktrace and code snippet available it seems your dbPortis chatserver, whereas port is an int like 3306, hence while parsing it as Integer it is failing and giving you NumberFormatException.

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

So instead of

所以代替

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

try with this

试试这个

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

Complete connection string will look like below:

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

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

In above you can place your username, password, dbPort, databasename e.t.c.

在上面你可以放置你的用户名、密码、dbPort、databasename 等

Here I am assuming you want to connect to a schema named as chatserver

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

回答by CamaroSS

I think the problem is in

我认为问题出在

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

You get a NumberFormatException, because port number should be numeric.

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

回答by Kumar Vivek Mitra

Look at my code below, and try to implement it as its here.... i hope it will run smoothly.. Once you start getting your desired query result, modify it to your heart content for further R&D.

看看我下面的代码,并尝试在这里实现它......我希望它能够顺利运行......一旦你开始得到你想要的查询结果,将其修改为你的核心内容以进行进一步的研发。

Please change the password and usernamein this program

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


    }