Inserting data into a SQL table problem

MYSQL null pointer exception error

Page 1 of 1

8 Replies - 1617 Views - Last Post: 17 July 2009 - 09:26 PM Rate Topic: -----

#1 javamad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 08-June 09

Inserting data into a SQL table problem

Post icon  Posted 17 July 2009 - 01:20 AM

Hi,

I am trying to insert data into a SQL table, but I keep getting the following error:

Error - SafeMySql: Exception: INSERT INTO country (country_id,name,description) VALUES (6,'AS','AS')
java.lang.NullPointerException
at Gui3.SQLExecuteUpdate.<init>(SQLExecuteUpdate.java:16)
at Gui3.UploadSimulationData.<init>(UploadSimulationData.java:30)
at Gui3.GuiMain.main(GuiMain.java:418)

I can connect to the database ok (as I am querying the database eleswhere for data).

the update that I am trying to upload is:
INSERT INTO country (country_id,name,description) VALUES (6,'AS','Holland')
all field names are present in the table

Could someone have a look at let me know how I can get data into the datase please. Please see below for code.

Many thanks

package Gui3;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class SQLExecuteUpdate {
	private Connection connection = null;
	public  SQLExecuteUpdate(SQLConn Uconn, String query) {
		int result = 0;

			try
			{
				Statement statement = null;
				statement = connection.createStatement();
				result = statement.executeUpdate(query);
				//dbConn.commit();
				connection.commit();
				statement.close();
			}

			catch (SQLException exSql)
			{
				System.err.println("Error - SafeMySql: SQL Exception: " + query);
				exSql.printStackTrace();
			}

			catch (Exception ex){

				System.err.println("Error - SafeMySql: Exception: " + query);
				ex.printStackTrace();
			}
		}
		
	}
	





package Gui3;

import java.util.HashMap;

public class UploadSimulationData {

	public UploadSimulationData(SQLConn Uconn,
			HashMap<String, Integer> dbDistinctCountryIDMap,
			HashMap<String, String> dbDistinctCountryNameMap,
			HashMap<String, String> dbDistinctCountryDescriptionMap) {

		int countryTableID;
		String countryTableName;
		String countryTableDescription;

		// upload countries
		for (Object key : dbDistinctCountryIDMap.keySet()) {

			countryTableID = dbDistinctCountryIDMap.get(key);
			countryTableName = dbDistinctCountryNameMap.get(key);
			countryTableDescription = dbDistinctCountryDescriptionMap.get(key);

			String query;
			
						
			query = "INSERT INTO country (country_id,name,description) VALUES ("+countryTableID+",'"+countryTableName+"','"+countryTableDescription+"')";
			
			
					
			SQLExecuteUpdate InstrumentUpdate = new SQLExecuteUpdate(Uconn,query);
			
			
			
		}

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Inserting data into a SQL table problem

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 01:31 AM

I think you need to initialize connection in you're SQLExecuteUpdate class
Was This Post Helpful? 0
  • +
  • -

#3 javamad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 08-June 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 02:20 AM

Thanks for the reply,

Could you let me know how I initialize my connection please (still very new to this)

Many thanks
Was This Post Helpful? 0
  • +
  • -

#4 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 02:29 AM

I would guess putting connection = Uconn; at the beginning of the constructor but I can't find any class called SQLConn in any documentation.
Was This Post Helpful? 0
  • +
  • -

#5 javamad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 08-June 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 02:39 AM

when I add

connection = Uconn

I get the following error:

Type mismatch: cannot convert from SQLConn to Connection

Also please see below for the SQLConn class.

Thanks for your help so far





package Gui3;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.*;
import com.mysql.jdbc.Driver; // this is for MySql access
import java.util.*;

public class SQLConn {

	/** Class fields */

	private Connection connection = null;
	private boolean isConnected = false;
	private boolean verbose = true;
	private static String SERVER = "xxx";
	private static String PORT = "xxx";
	private static String DB = "xx";
	private static String USER = "xx";
	private static String PWD = "xx";
	private String server = SERVER;
	private String port = PORT;
	private String database = DB;
	private String user = USER;
	private String pwd = PWD;

	/** Constructor - creates a new instance of SafeMySql */

	public SQLConn(String serverName, String portNumber, String userName, String password, String databaseName, boolean giveOutput) {

			verbose = giveOutput;

		Properties props = new Properties();

		String url = "jdbc:mysql://";

		

		if (serverName != null)

			server = serverName;

		if (portNumber != null)

			port = portNumber;

		if (userName != null)

			user = userName;

		if (password != null)

			pwd = password;

		if (databaseName != null)

			database = databaseName;

		String sqlConnectionStr = url + server + ":" + port + "/" + database;

		try

		{

			Driver mySqlDriver = new Driver();

			props.put("user", user);

			props.put("password", pwd);

			if (verbose)

						System.out.println("Info - SafeMySql: Connecting to DB " + database + " at " + server + " on port " + port);

			connection = mySqlDriver.connect(sqlConnectionStr,props);

			if (connection != null)

				isConnected = true;

		}

		catch (SQLException exSql)

		{

			System.err.println("Error - SafeMySql: SQL Exception ");

			exSql.printStackTrace();

			isConnected = false;

			System.err.println("Error - SafeMySql: Not Connected ");

		}

		catch (Exception ex)

		{

			System.err.println("Error - SafeMySql: Exception ");

			ex.printStackTrace();

			isConnected = false;

			System.err.println("Error - SafeMySql: Not Connected ");

		}

		

		if (isConnected) 

			System.out.println("Connected to " + server);

		else

			System.out.println("NOT Connected");

	}
}

	

Was This Post Helpful? 0
  • +
  • -

#6 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 02:51 AM

All right you are going to have to make a get method for connection in you're SQLConn class and then add
connection = Uconn.getConnection();
To the constructor of you're SQLExecuteUpdate Class.

You're problem is that you never assign a value to connection. This line of code gets the connection from the SQLConn class that you passed in the constructor.
Was This Post Helpful? 1
  • +
  • -

#7 javamad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 08-June 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 03:02 AM

Thanks

Sorry about all this...
I have created the method

public Connection getConnection() {
		
		return null;
	}



in the SQLConn. But don't know what to put in it!

have also added :

connection = Uconn.getConnection();



to the SQLExecuteUpdate class

Could you let me know what I need to do now as still getting the same error

Many thanks
Was This Post Helpful? 0
  • +
  • -

#8 javamad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 70
  • Joined: 08-June 09

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 03:25 AM

Thanks for your help it works now
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Inserting data into a SQL table problem

Posted 17 July 2009 - 09:26 PM

Good show Syfran :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1