13 Replies - 3490 Views - Last Post: 06 September 2011 - 04:55 AM Rate Topic: -----

#1 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

[SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 07:53 AM

Ok so I'm making a plugin and here is the file that handles the database stuff... It's spitting out tonnes of errors at me and I'm sort of overwhelmed by it xD I'd be grateful if i could get some help, the errors and code are below :/

[SEVERE] com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "null"'.
2011-09-05 19:09:37 [SEVERE] 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
2011-09-05 19:09:37 [SEVERE] 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
2011-09-05 19:09:37 [SEVERE] 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
2011-09-05 19:09:37 [SEVERE] 	at java.lang.reflect.Constructor.newInstance(Unknown Source)
2011-09-05 19:09:37 [SEVERE] 	at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.Util.getInstance(Util.java:382)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
2011-09-05 19:09:37 
[SEVERE] 	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:314)
2011-09-05 19:09:37 
[SEVERE] 	at java.sql.DriverManager.getConnection(Unknown Source)
2011-09-05 19:09:37 
[SEVERE] 	at java.sql.DriverManager.getConnection(Unknown Source)
2011-09-05 19:09:37 



package me.kamisoyokaze.mcscores;

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

public class database {
	MCscores plugin = new MCscores();
	
	private String host = plugin.getConfig_host();
	private String port = plugin.getConfig_port();
	private String database = plugin.getConfig_database();
	private String user = plugin.getConfig_user();
	private String password = plugin.getConfig_password();

	public int retrieve(String name) throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);

		PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + name + ";");
		//Query

		ResultSet result = statement.executeQuery();
		//variable to execute query
		int score = result.getInt(3);
		return score;		
	}

	public void enter(String kname) throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);

		boolean exists = false;

		PreparedStatement existing = con.prepareStatement("SELECT EXISTS(SELECT * FROM highscores WHERE name = " + kname + ")");

		if(existing.executeQuery() != null) {
			exists = true;
		}

		if(exists){
			PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + kname + ";");

			ResultSet result = statement.executeQuery();
			int score = result.getInt(3);
			int newscore = score++;

			PreparedStatement changeScore = con.prepareStatement("UPDATE  `highscores`.`highscores` SET  `score` =  " + newscore + "' WHERE  `highscores`.`name` =  'bawb'");
			changeScore.executeQuery();
			System.out.println(kname + " score updated!");
		}
		else {
			PreparedStatement addRecord = con.prepareStatement("INSERT INTO  `highscores`.`highscores` (`id` ,`name` ,`score`) VALUES (NULL ,  " + kname + "',  '1');");
			addRecord.executeQuery();
			System.out.println(kname + " score updated!");
		}
	}
}


This post has been edited by PixelBit: 06 September 2011 - 05:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED]Tonnes of JDBC and Javasql Errors.

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:10 AM

Are you encountering any specific error messages? I don't see anything right off the bat from this class. Where do you use it?
Was This Post Helpful? 0
  • +
  • -

#3 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:13 AM

View Postmacosxnerd101, on 05 September 2011 - 08:10 AM, said:

Are you encountering any specific error messages? I don't see anything right off the bat from this class. Where do you use it?

It's part of a plugin I'm making for the game minecraft (lol). One thing i was conscious about was the fact that i have no idea where to put the JDBC JAR, should i put it inside the jar I made out of the collection of these classes? Or should i put it in the folder the Jar i made is in, I decided to put it in the folder the Jar is in.

Oh and no, no error messages, just simply potato like behavior.

This post has been edited by PixelBit: 05 September 2011 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:17 AM

Wherever you place the JAR file on your computer, you will have to add it to the project. Which IDE are you using?

Quote

Oh and no, no error messages, just simply potato like behavior.

How are you using the class? It won't do anything if it isn't used.
Was This Post Helpful? 0
  • +
  • -

#5 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:24 AM

View Postmacosxnerd101, on 05 September 2011 - 08:17 AM, said:

Wherever you place the JAR file on your computer, you will have to add it to the project. Which IDE are you using?

Quote

Oh and no, no error messages, just simply potato like behavior.

How are you using the class? It won't do anything if it isn't used.

Well I'm using eclipse for this, i added JDBC to the build path if that's what you mean? Also as for using the class its methods are called upon by another class which is called upon by the game when a player is killed.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:26 AM

I simply don't have enough information to help you. I don't see how it's being used, etc.
Was This Post Helpful? 1
  • +
  • -

#7 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 08:30 AM

View Postmacosxnerd101, on 05 September 2011 - 08:26 AM, said:

I simply don't have enough information to help you. I don't see how it's being used, etc.

Ok thanks anyway, i think i'm one to something now though.
Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 10:09 AM

addRecord.executeQuery();
Try changing that to .executeUpdate() . You use the executeUpdate() method of the statement object when we don't expect any data to be returned. This is when we create databases or execute INSERT, UPDATE, DELETE statements. I am somewhat new to MySQL myself, but I believe you only use query() when it is a regular Statement and not a PreparedStatement

This post has been edited by Fuzzyness: 05 September 2011 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#9 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 11:23 AM

post updated with my new troubles. This is really getting on my nerves xD It's an endless stream of problems.
Was This Post Helpful? 0
  • +
  • -

#10 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 01:43 PM

[SEVERE]    at java.sql.DriverManager.getConnection(Unknown Source)


So the error occurred at DriverManager.getConnection().

Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);


According to the API you can either send the database url or the url, user name, and password to the constructor, but you've concatenated the url, user name, and password and sent it to the one-arg constructor.

getConnection(String url)
getConnection(String url, String user, String password)



You think this could be why the error occurs? Try this:
Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);

Was This Post Helpful? 1
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 02:04 PM

View PostFuzzyness, on 05 September 2011 - 01:09 PM, said:

addRecord.executeQuery();
You use the executeUpdate() method of the statement object when we don't expect any data to be returned.

Yes it returns the number of rows affected :)
Was This Post Helpful? 1
  • +
  • -

#12 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 02:28 PM

Thanks guys I'll look this over tomorrow! I'm limited to my ipad at the moment.
Was This Post Helpful? 0
  • +
  • -

#13 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 05 September 2011 - 03:51 PM

Problably got an issue with your connection, but you would need to print out the values returned when you retrieve the hosts etc to make sure its not adding extra white spaces or anything. You should tidy it up a bit anyway, and have a class handle all your database actions such as
import java.sql.Connection;  //the importing of required libraries
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**This class contains all methods which relate to database connection, closing and exception handling*/
public class DatabaseUtils
{   
    /**method which returns a connection*/
	public static Connection connect(String driver, String url) throws ClassNotFoundException, SQLException //exception handling
	{
		Class.forName(driver);  //passing the driver to Class.forName
		return DriverManager.getConnection(url);  //passing the reamining variables and being returned a connection
	}
	
	/**Method which closes a connectio*/
	public static void close(Connection connection)  
	{
		try  //try block (try doing this)
		{
			if (connection != null) //if connection is not equal to null (if there is a connection)
			{
				connection.close();  //close it
			}
		
		}
		catch (SQLException e)  //catch block to handle an exception
		{
			e.printStackTrace(); //print exception to the command line
		}
		
	}
	
	/**Method which closes a ResultSet*/
	public static void close(ResultSet resultSet)  
	{
		try  //try
		{
			if (resultSet != null)  //if resultset not equal to null
			{
				resultSet.close(); //close it
			}
		}
		catch (SQLException e)  //catch
		{
			e.printStackTrace();
		}
	}
	
	/**Method which closes a Statement*/
	public static void close(Statement statement)  
	{
		try
		{
			if (statement != null)
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	/**Method which rolls back the connection*/
	public static void rollback(Connection connection) 
	{
		try
		{
			if (connection != null)
			{
				connection.rollback();
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	
	
}
 


Then in the class your working in, you can do something like
private Connection con = null;   //initialising an empty connection
	
private static final String DRIVER = plugin.getConfig_host();
private static final String URL = plugin.getConfig_database();

con = DatabaseUtils.connect(DRIVER, URL);   


etc etc etc

This post has been edited by nick2price: 05 September 2011 - 03:57 PM

Was This Post Helpful? 1
  • +
  • -

#14 PixelBit   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 233
  • Joined: 17-July 11

Re: [SOLVED]Tonnes of JDBC and Javasql Errors.

Posted 06 September 2011 - 04:55 AM

Ok thanks for all the help guys it turns out i had to do this to get rid of that error:

Connection con = DriverManager.getConnection("\"jdbc:mysql://" + host + ":" + port + "/" + database + "\"", "\"" + user + "\"" , "\"" +password + "\"");

This post has been edited by PixelBit: 06 September 2011 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1