Hi all,
I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another method and display the result as an arraylist. The method and the object are as below:
public static ArrayList getProjects(int userID)
throws SQLException, Exception
{
ArrayList arrayList = null;
getProjectsStmt .setInt(1, userID);
ResultSet rs = getProjectsStmt .executeQuery() ;
arrayList = new ArrayList();
try
{
while (rs.next())
{
Projects p = createProject(r s);
arrayList.add(p );
rs.close();
}
}
catch(Exception e)
{
System.err.prin tln(e);
}
return arrayList;
}
//Query is as follows
private static final String GET_PROJECTS_ST MT = "SELECT projectID, name FROM project WHERE userID = ?";
//createProject method that creates the Project object is as below
private static Projects createProject(R esultSet rs)
throws SQLException, Exception
{
int projectID = rs.getInt("proj ectID");
int userID = rs.getInt("user ID");
String name = rs.getString("n ame");
String description = rs.getString("d escription");
int createdate = rs.getInt("crea tedate");
boolean visibility = rs.getBoolean(" visibility");
return new Projects(projec tID, userID, name, description, createdate, visibility);
}
When i try and run this method in main to test it, i.e. getProject(1); the entry 1 exists in database and the results of the query also exists but I still get the error saying java.lang.NullP ointerException
It points to the following lines:
getProjectsStmt .setInt(1, userID); - line 6 in the getProjects method - above
getProject(1) - in main where I try to test it
Any ideas why???
Thanks,
dev
6 4197
Hi all,
I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another method and display the result as an arraylist. The method and the object are as below:
public static ArrayList getProjects(int userID)
throws SQLException, Exception
{
ArrayList arrayList = null;
getProjectsStmt .setInt(1, userID);
ResultSet rs = getProjectsStmt .executeQuery() ;
arrayList = new ArrayList();
try
{
while (rs.next())
{
Projects p = createProject(r s);
arrayList.add(p );
rs.close();
}
}
catch(Exception e)
{
System.err.prin tln(e);
}
return arrayList;
}
//Query is as follows
private static final String GET_PROJECTS_ST MT = "SELECT projectID, name FROM project WHERE userID = ?";
//createProject method that creates the Project object is as below
private static Projects createProject(R esultSet rs)
throws SQLException, Exception
{
int projectID = rs.getInt("proj ectID");
int userID = rs.getInt("user ID");
String name = rs.getString("n ame");
String description = rs.getString("d escription");
int createdate = rs.getInt("crea tedate");
boolean visibility = rs.getBoolean(" visibility");
return new Projects(projec tID, userID, name, description, createdate, visibility);
}
When i try and run this method in main to test it, i.e. getProject(1); the entry 1 exists in database and the results of the query also exists but I still get the error saying java.lang.NullP ointerException
It points to the following lines:
getProjectsStmt .setInt(1, userID); - line 6 in the getProjects method - above
getProject(1) - in main where I try to test it
Any ideas why???
Thanks,
dev
1.Use code tags when posting code
2.Are you using GET_PROJECTS_ST MT or getProjectsStmt ?
1.Use code tags when posting code
2.Are you using GET_PROJECTS_ST MT or getProjectsStmt ?
Thanks for reply.. Sorry I'll bear that in mind... Basically there is a method prepareQueries( ) where I initialise getProjectsStmt as follows: -
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
-
-
If it helps the complete code is as follows: -
-
import java.net.*;
-
import java.io.*;
-
import java.util.*;
-
import java.io.OutputStream;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import database.Users;
-
import database.Projects;
-
import database.Diagram;
-
import JavaSVN.ExtractText;
-
import java.sql.Statement;
-
-
public class QuerySourceData
-
{
-
-
private static Connection con;
-
private static Object connectionLock = new Object();
-
-
private static PreparedStatement getProjectsStmt;
-
private static PreparedStatement getNameStmt;
-
private static PreparedStatement addUserStmt;
-
private static PreparedStatement addProjectStmt;
-
private static PreparedStatement addDiagramStmt;
-
private static PreparedStatement getfilePathStmt;
-
-
private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
-
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
-
private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
-
private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
-
private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
-
private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
-
-
public QuerySourceData() {}
-
-
private static void prepareQueries()
-
throws SQLException, Exception
-
{
-
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
-
-
getNameStmt = con.prepareStatement(GET_NAME_STMT);
-
-
}
-
-
//enables connection to the database to perform the queries and updates
-
public static void connect()
-
throws SQLException, Exception
-
{
-
synchronized (connectionLock)
-
{
-
if (con != null)
-
{
-
throw new IllegalStateException("Already connected to database, close first, then re-connect");
-
}
-
-
try
-
{
-
Properties props = new Properties();
-
FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
-
props.load(in);
-
String driver = props.getProperty("jdbc.driver");
-
Class.forName(driver);
-
String url = props.getProperty("jdbc.url");
-
String username = props.getProperty("jdbc.username");
-
String password = props.getProperty("jdbc.password");
-
in.close();
-
}
-
-
catch(Exception e)
-
{
-
System.err.println("Exception: " + e);
-
}
-
-
}
-
-
}
-
-
// determines if it is still connected to the database - designed for expansion
-
public static boolean isConnected()
-
{
-
synchronized (connectionLock)
-
{
-
return con != null;
-
}
-
}
-
-
// closes connection to the database - designed for expansion
-
public static void close()
-
throws SQLException, Exception
-
{
-
synchronized (connectionLock)
-
{
-
con.close();
-
con = null;
-
}
-
}
-
-
public static void addUser(Users u)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addUserStmt.setInt(1, u.getuserID());
-
addUserStmt.setString(2, u.getTitle());
-
addUserStmt.setString(3, u.getfirstName());
-
addUserStmt.setString(4, u.getlastName());
-
addUserStmt.setString(5, u.getEmail());
-
addUserStmt.setBoolean(6, u.getAdmin());
-
addUserStmt.setBoolean(7, u.getcreateProject());
-
-
addUserStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addProject(Projects p)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addProjectStmt.setInt(1, p.getprojectID());
-
addProjectStmt.setInt(2, p.getuserID());
-
addProjectStmt.setString(3, p.getprojectName());
-
addProjectStmt.setString(4, p.getDescription());
-
addProjectStmt.setInt(5, p.getcreateDate());
-
addProjectStmt.setBoolean(6, p.getVisibility());
-
-
addProjectStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addDiagram(Diagram d)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addDiagramStmt.setInt(1, d.getdiagramID());
-
addDiagramStmt.setInt(2, d.getuserID());
-
addDiagramStmt.setInt(3, d.getprojectID());
-
addDiagramStmt.setString(4, d.getdiagramName());
-
addDiagramStmt.setString(5, d.getDescription());
-
addDiagramStmt.setString(6, d.getSummary());
-
addDiagramStmt.setString(7, d.getfilePath());
-
addDiagramStmt.setString(8, d.getdiagramUrl());
-
addDiagramStmt.setInt(9, d.getcreateDate());
-
-
addDiagramStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
// gets names and IDs of projects in which user is involved from userID
-
public static ArrayList getProjects(int userID)
-
throws SQLException, Exception
-
{
-
ArrayList arrayList = null;
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
-
getProjectsStmt.setInt(1, userID);
-
ResultSet rs = getProjectsStmt.executeQuery();
-
arrayList = new ArrayList();
-
-
try
-
{
-
while (rs.next())
-
{
-
Projects p = createProject(rs);
-
arrayList.add(p);
-
rs.close();
-
-
}
-
}
-
catch(Exception e)
-
{
-
System.err.println(e + ": UserID entered has not been found.");
-
}
-
-
return arrayList;
-
-
}
-
-
-
private static Projects createProject(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int projectID = rs.getInt("projectID");
-
int userID = rs.getInt("userID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
int createdate = rs.getInt("createdate");
-
boolean visibility = rs.getBoolean("visibility");
-
-
return new Projects(projectID, userID, name, description, createdate, visibility);
-
}
-
-
private static Diagram createDiagram(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int diagramID = rs.getInt("diagramID");
-
int userID = rs.getInt("userID");
-
int projectID = rs.getInt("projectID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
String summary = rs.getString("summary");
-
String filePath = rs.getString("filePath");
-
String diagramUrl = rs.getString("diagramUrl");
-
int createdate = rs.getInt("createdate");
-
-
return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
-
}
-
-
private static Users createUsers(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int userID = rs.getInt("userID");
-
String title = rs.getString("title");
-
String firstname = rs.getString("firstname");
-
String lastname = rs.getString("lastname");
-
String email = rs.getString("email");
-
boolean admin = rs.getBoolean("admin");
-
boolean createProject = rs.getBoolean("createProject");
-
-
return new Users(userID, title, firstname, lastname, email, admin, createProject);
-
}
-
-
public static void main(String[] args)
-
throws SQLException, Exception
-
{
-
-
connect();
-
prepareQueries();
-
getProjects(1);
-
-
}
-
}
-
Thanks!!
Dev
Thanks for reply.. Sorry I'll bear that in mind... Basically there is a method prepareQueries( ) where I initialise getProjectsStmt as follows: -
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
-
-
If it helps the complete code is as follows: -
-
import java.net.*;
-
import java.io.*;
-
import java.util.*;
-
import java.io.OutputStream;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import database.Users;
-
import database.Projects;
-
import database.Diagram;
-
import JavaSVN.ExtractText;
-
import java.sql.Statement;
-
-
public class QuerySourceData
-
{
-
-
private static Connection con;
-
private static Object connectionLock = new Object();
-
-
private static PreparedStatement getProjectsStmt;
-
private static PreparedStatement getNameStmt;
-
private static PreparedStatement addUserStmt;
-
private static PreparedStatement addProjectStmt;
-
private static PreparedStatement addDiagramStmt;
-
private static PreparedStatement getfilePathStmt;
-
-
private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
-
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
-
private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
-
private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
-
private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
-
private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
-
-
public QuerySourceData() {}
-
-
private static void prepareQueries()
-
throws SQLException, Exception
-
{
-
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
-
-
getNameStmt = con.prepareStatement(GET_NAME_STMT);
-
-
}
-
-
//enables connection to the database to perform the queries and updates
-
public static void connect()
-
throws SQLException, Exception
-
{
-
synchronized (connectionLock)
-
{
-
if (con != null)
-
{
-
throw new IllegalStateException("Already connected to database, close first, then re-connect");
-
}
-
-
try
-
{
-
Properties props = new Properties();
-
FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
-
props.load(in);
-
String driver = props.getProperty("jdbc.driver");
-
Class.forName(driver);
-
String url = props.getProperty("jdbc.url");
-
String username = props.getProperty("jdbc.username");
-
String password = props.getProperty("jdbc.password");
-
in.close();
-
}
-
-
catch(Exception e)
-
{
-
System.err.println("Exception: " + e);
-
}
-
-
}
-
-
}
-
-
// determines if it is still connected to the database - designed for expansion
-
public static boolean isConnected()
-
{
-
synchronized (connectionLock)
-
{
-
return con != null;
-
}
-
}
-
-
// closes connection to the database - designed for expansion
-
public static void close()
-
throws SQLException, Exception
-
{
-
synchronized (connectionLock)
-
{
-
con.close();
-
con = null;
-
}
-
}
-
-
public static void addUser(Users u)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addUserStmt.setInt(1, u.getuserID());
-
addUserStmt.setString(2, u.getTitle());
-
addUserStmt.setString(3, u.getfirstName());
-
addUserStmt.setString(4, u.getlastName());
-
addUserStmt.setString(5, u.getEmail());
-
addUserStmt.setBoolean(6, u.getAdmin());
-
addUserStmt.setBoolean(7, u.getcreateProject());
-
-
addUserStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addProject(Projects p)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addProjectStmt.setInt(1, p.getprojectID());
-
addProjectStmt.setInt(2, p.getuserID());
-
addProjectStmt.setString(3, p.getprojectName());
-
addProjectStmt.setString(4, p.getDescription());
-
addProjectStmt.setInt(5, p.getcreateDate());
-
addProjectStmt.setBoolean(6, p.getVisibility());
-
-
addProjectStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addDiagram(Diagram d)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addDiagramStmt.setInt(1, d.getdiagramID());
-
addDiagramStmt.setInt(2, d.getuserID());
-
addDiagramStmt.setInt(3, d.getprojectID());
-
addDiagramStmt.setString(4, d.getdiagramName());
-
addDiagramStmt.setString(5, d.getDescription());
-
addDiagramStmt.setString(6, d.getSummary());
-
addDiagramStmt.setString(7, d.getfilePath());
-
addDiagramStmt.setString(8, d.getdiagramUrl());
-
addDiagramStmt.setInt(9, d.getcreateDate());
-
-
addDiagramStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
// gets names and IDs of projects in which user is involved from userID
-
public static ArrayList getProjects(int userID)
-
throws SQLException, Exception
-
{
-
ArrayList arrayList = null;
-
getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
-
getProjectsStmt.setInt(1, userID);
-
ResultSet rs = getProjectsStmt.executeQuery();
-
arrayList = new ArrayList();
-
-
try
-
{
-
while (rs.next())
-
{
-
Projects p = createProject(rs);
-
arrayList.add(p);
-
rs.close();
-
-
}
-
}
-
catch(Exception e)
-
{
-
System.err.println(e + ": UserID entered has not been found.");
-
}
-
-
return arrayList;
-
-
}
-
-
-
private static Projects createProject(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int projectID = rs.getInt("projectID");
-
int userID = rs.getInt("userID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
int createdate = rs.getInt("createdate");
-
boolean visibility = rs.getBoolean("visibility");
-
-
return new Projects(projectID, userID, name, description, createdate, visibility);
-
}
-
-
private static Diagram createDiagram(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int diagramID = rs.getInt("diagramID");
-
int userID = rs.getInt("userID");
-
int projectID = rs.getInt("projectID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
String summary = rs.getString("summary");
-
String filePath = rs.getString("filePath");
-
String diagramUrl = rs.getString("diagramUrl");
-
int createdate = rs.getInt("createdate");
-
-
return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
-
}
-
-
private static Users createUsers(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int userID = rs.getInt("userID");
-
String title = rs.getString("title");
-
String firstname = rs.getString("firstname");
-
String lastname = rs.getString("lastname");
-
String email = rs.getString("email");
-
boolean admin = rs.getBoolean("admin");
-
boolean createProject = rs.getBoolean("createProject");
-
-
return new Users(userID, title, firstname, lastname, email, admin, createProject);
-
}
-
-
public static void main(String[] args)
-
throws SQLException, Exception
-
{
-
-
connect();
-
prepareQueries();
-
getProjects(1);
-
-
}
-
}
-
Thanks!!
Dev
I have to go but look through your code and see how you intialized con - private static Connection con;
Your constructor does not initialize it either so it's probably null when you want to use it
I have to go but look through your code and see how you intialized con - private static Connection con;
Your constructor does not initialize it either so it's probably null when you want to use it
Hi,
Thanks a lot for your help. I initialized connection object in a constructor which I called in main and now it is connecting to the database. Now the queries are playing up but I think I can handle it.
Thanks a lot for your help again.
Dev
Hi,
Now the queries are playing up but I think I can handle it.
Thanks a lot for your help again.
Dev
May be not...
I just cant figure out what am i doing wrong.. I have tried different things but keep getting exceptions... I even tried system.out.prin tln to try and print the resultset rs.getString but it bypasses everything and goes into error... I have rewritten the code which is as follows -
-
package database;
-
-
import java.net.*;
-
import java.io.*;
-
import java.util.*;
-
import java.io.OutputStream;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import database.Users;
-
import database.Project;
-
import database.Diagram;
-
import JavaSVN.ExtractText;
-
import java.sql.Statement;
-
-
-
public class QuerySourceData
-
{
-
private ExtractText ET;
-
-
-
private static Object connectionLock = new Object();
-
-
private static PreparedStatement getProjectsStmt;
-
private static PreparedStatement getNameStmt;
-
private static PreparedStatement addUserStmt;
-
private static PreparedStatement addProjectStmt;
-
private static PreparedStatement addDiagramStmt;
-
private static PreparedStatement getfilePathStmt;
-
-
private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
-
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
-
private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
-
-
private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
-
private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
-
private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
-
-
-
public QuerySourceData()
-
throws Exception
-
{
-
try
-
{
-
Properties props = new Properties();
-
FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
-
props.load(in);
-
String driver = props.getProperty("jdbc.driver");
-
Class.forName(driver);
-
String url = props.getProperty("jdbc.url");
-
String username = props.getProperty("jdbc.username");
-
String password = props.getProperty("jdbc.password");
-
in.close();
-
}
-
-
catch(Exception e)
-
{
-
System.err.println("Exception: " + e);
-
}
-
-
try
-
{
-
Connection conn = QuerySourceData.getConnection();
-
-
getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
-
getNameStmt = conn.prepareStatement(GET_NAME_STMT); //gets names of diagrams from projectID
-
getfilePathStmt = conn.prepareStatement(GET_FILEPATH_STMT);
-
}
-
catch(Exception e)
-
{
-
System.out.println(e);
-
}
-
}
-
-
-
-
public static void addUser(Users u)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addUserStmt.setInt(1, u.getuserID());
-
addUserStmt.setString(2, u.getTitle());
-
addUserStmt.setString(3, u.getfirstName());
-
addUserStmt.setString(4, u.getlastName());
-
addUserStmt.setString(5, u.getEmail());
-
addUserStmt.setBoolean(6, u.getAdmin());
-
addUserStmt.setBoolean(7, u.getcreateProject());
-
-
addUserStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addProject(Project p)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addProjectStmt.setInt(1, p.getprojectID());
-
addProjectStmt.setInt(2, p.getuserID());
-
addProjectStmt.setString(3, p.getprojectName());
-
addProjectStmt.setString(4, p.getDescription());
-
addProjectStmt.setInt(5, p.getcreateDate());
-
addProjectStmt.setBoolean(6, p.getVisibility());
-
-
addProjectStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addDiagram(Diagram d)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addDiagramStmt.setInt(1, d.getdiagramID());
-
addDiagramStmt.setInt(2, d.getuserID());
-
addDiagramStmt.setInt(3, d.getprojectID());
-
addDiagramStmt.setString(4, d.getdiagramName());
-
addDiagramStmt.setString(5, d.getDescription());
-
addDiagramStmt.setString(6, d.getSummary());
-
addDiagramStmt.setString(7, d.getfilePath());
-
addDiagramStmt.setString(8, d.getdiagramUrl());
-
addDiagramStmt.setInt(9, d.getcreateDate());
-
-
addDiagramStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
-
-
public static String getDiagrams(int projectID)
-
throws SQLException, Exception
-
{
-
new QuerySourceData();
-
Connection conn = QuerySourceData.getConnection();
-
getNameStmt = conn.prepareStatement(GET_NAME_STMT);
-
getNameStmt.setInt(1, projectID);
-
ResultSet rs = getNameStmt.executeQuery();
-
-
if(rs.first())
-
{
-
Diagram d = createDiagram(rs);
-
rs.close();
-
conn.close();
-
String a = d.toString();
-
return a;
-
}
-
else
-
{
-
throw new Exception("An error occured!");
-
}
-
-
}
-
-
// gets names and IDs of projects in which user is involved from userID
-
public static void getProjects(int userID)
-
throws SQLException, Exception
-
{
-
new QuerySourceData();
-
Connection conn = QuerySourceData.getConnection();
-
getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT);
-
getProjectsStmt.setInt(1, userID);
-
ResultSet rs = getProjectsStmt.executeQuery();
-
-
if(rs.first())
-
{
-
Project p = createProject(rs);
-
rs.close();
-
conn.close();
-
String a = p.toString();
-
System.out.println(a);
-
//return a;
-
}
-
-
else
-
{
-
throw new Exception("An error occured");
-
}
-
}
-
-
private static Project createProject(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int projectID = rs.getInt("projectID");
-
int userID = rs.getInt("userID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
int createdate = rs.getInt("createdate");
-
boolean visibility = rs.getBoolean("visibility");
-
-
return new Project(projectID, userID, name, description, createdate, visibility);
-
}
-
-
private static Diagram createDiagram(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int diagramID = rs.getInt("diagramID");
-
int userID = rs.getInt("userID");
-
int projectID = rs.getInt("projectID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
String summary = rs.getString("summary");
-
String filePath = rs.getString("filePath");
-
String diagramUrl = rs.getString("diagramUrl");
-
int createdate = rs.getInt("createdate");
-
-
return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
-
}
-
-
private static Users createUsers(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int userID = rs.getInt("userID");
-
String title = rs.getString("title");
-
String firstname = rs.getString("firstname");
-
String lastname = rs.getString("lastname");
-
String email = rs.getString("email");
-
boolean admin = rs.getBoolean("admin");
-
boolean createProject = rs.getBoolean("createProject");
-
-
return new Users(userID, title, firstname, lastname, email, admin, createProject);
-
}
-
-
public static void main(String[] args)
-
throws SQLException, Exception
-
{
-
-
getProjects(2);
-
getDiagrams(3);
-
}
-
-
public static Connection getConnection()
-
throws SQLException
-
{
-
return DriverManager.getConnection("jdbc:postgresql://dbteach/", "ug73dxs", "thowrisw");
-
}
-
}
-
-
Why r resultsets not providing the same results as when i try them through direct code in main???
Thanks...
May be not...
I just cant figure out what am i doing wrong.. I have tried different things but keep getting exceptions... I even tried system.out.prin tln to try and print the resultset rs.getString but it bypasses everything and goes into error... I have rewritten the code which is as follows -
-
package database;
-
-
import java.net.*;
-
import java.io.*;
-
import java.util.*;
-
import java.io.OutputStream;
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import database.Users;
-
import database.Project;
-
import database.Diagram;
-
import JavaSVN.ExtractText;
-
import java.sql.Statement;
-
-
-
public class QuerySourceData
-
{
-
private ExtractText ET;
-
-
-
private static Object connectionLock = new Object();
-
-
private static PreparedStatement getProjectsStmt;
-
private static PreparedStatement getNameStmt;
-
private static PreparedStatement addUserStmt;
-
private static PreparedStatement addProjectStmt;
-
private static PreparedStatement addDiagramStmt;
-
private static PreparedStatement getfilePathStmt;
-
-
private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
-
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
-
private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
-
-
private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
-
private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
-
private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
-
-
-
public QuerySourceData()
-
throws Exception
-
{
-
try
-
{
-
Properties props = new Properties();
-
FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
-
props.load(in);
-
String driver = props.getProperty("jdbc.driver");
-
Class.forName(driver);
-
String url = props.getProperty("jdbc.url");
-
String username = props.getProperty("jdbc.username");
-
String password = props.getProperty("jdbc.password");
-
in.close();
-
}
-
-
catch(Exception e)
-
{
-
System.err.println("Exception: " + e);
-
}
-
-
try
-
{
-
Connection conn = QuerySourceData.getConnection();
-
-
getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
-
getNameStmt = conn.prepareStatement(GET_NAME_STMT); //gets names of diagrams from projectID
-
getfilePathStmt = conn.prepareStatement(GET_FILEPATH_STMT);
-
}
-
catch(Exception e)
-
{
-
System.out.println(e);
-
}
-
}
-
-
-
-
public static void addUser(Users u)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addUserStmt.setInt(1, u.getuserID());
-
addUserStmt.setString(2, u.getTitle());
-
addUserStmt.setString(3, u.getfirstName());
-
addUserStmt.setString(4, u.getlastName());
-
addUserStmt.setString(5, u.getEmail());
-
addUserStmt.setBoolean(6, u.getAdmin());
-
addUserStmt.setBoolean(7, u.getcreateProject());
-
-
addUserStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addProject(Project p)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addProjectStmt.setInt(1, p.getprojectID());
-
addProjectStmt.setInt(2, p.getuserID());
-
addProjectStmt.setString(3, p.getprojectName());
-
addProjectStmt.setString(4, p.getDescription());
-
addProjectStmt.setInt(5, p.getcreateDate());
-
addProjectStmt.setBoolean(6, p.getVisibility());
-
-
addProjectStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
public static void addDiagram(Diagram d)
-
throws SQLException
-
{
-
-
synchronized (connectionLock)
-
{
-
try
-
{
-
addDiagramStmt.setInt(1, d.getdiagramID());
-
addDiagramStmt.setInt(2, d.getuserID());
-
addDiagramStmt.setInt(3, d.getprojectID());
-
addDiagramStmt.setString(4, d.getdiagramName());
-
addDiagramStmt.setString(5, d.getDescription());
-
addDiagramStmt.setString(6, d.getSummary());
-
addDiagramStmt.setString(7, d.getfilePath());
-
addDiagramStmt.setString(8, d.getdiagramUrl());
-
addDiagramStmt.setInt(9, d.getcreateDate());
-
-
addDiagramStmt.executeUpdate();
-
}
-
-
catch (SQLException e)
-
{
-
throw e;
-
}
-
}
-
}
-
-
-
-
public static String getDiagrams(int projectID)
-
throws SQLException, Exception
-
{
-
new QuerySourceData();
-
Connection conn = QuerySourceData.getConnection();
-
getNameStmt = conn.prepareStatement(GET_NAME_STMT);
-
getNameStmt.setInt(1, projectID);
-
ResultSet rs = getNameStmt.executeQuery();
-
-
if(rs.first())
-
{
-
Diagram d = createDiagram(rs);
-
rs.close();
-
conn.close();
-
String a = d.toString();
-
return a;
-
}
-
else
-
{
-
throw new Exception("An error occured!");
-
}
-
-
}
-
-
// gets names and IDs of projects in which user is involved from userID
-
public static void getProjects(int userID)
-
throws SQLException, Exception
-
{
-
new QuerySourceData();
-
Connection conn = QuerySourceData.getConnection();
-
getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT);
-
getProjectsStmt.setInt(1, userID);
-
ResultSet rs = getProjectsStmt.executeQuery();
-
-
if(rs.first())
-
{
-
Project p = createProject(rs);
-
rs.close();
-
conn.close();
-
String a = p.toString();
-
System.out.println(a);
-
//return a;
-
}
-
-
else
-
{
-
throw new Exception("An error occured");
-
}
-
}
-
-
private static Project createProject(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int projectID = rs.getInt("projectID");
-
int userID = rs.getInt("userID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
int createdate = rs.getInt("createdate");
-
boolean visibility = rs.getBoolean("visibility");
-
-
return new Project(projectID, userID, name, description, createdate, visibility);
-
}
-
-
private static Diagram createDiagram(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int diagramID = rs.getInt("diagramID");
-
int userID = rs.getInt("userID");
-
int projectID = rs.getInt("projectID");
-
String name = rs.getString("name");
-
String description = rs.getString("description");
-
String summary = rs.getString("summary");
-
String filePath = rs.getString("filePath");
-
String diagramUrl = rs.getString("diagramUrl");
-
int createdate = rs.getInt("createdate");
-
-
return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
-
}
-
-
private static Users createUsers(ResultSet rs)
-
throws SQLException, Exception
-
{
-
-
int userID = rs.getInt("userID");
-
String title = rs.getString("title");
-
String firstname = rs.getString("firstname");
-
String lastname = rs.getString("lastname");
-
String email = rs.getString("email");
-
boolean admin = rs.getBoolean("admin");
-
boolean createProject = rs.getBoolean("createProject");
-
-
return new Users(userID, title, firstname, lastname, email, admin, createProject);
-
}
-
-
public static void main(String[] args)
-
throws SQLException, Exception
-
{
-
-
getProjects(2);
-
getDiagrams(3);
-
}
-
-
public static Connection getConnection()
-
throws SQLException
-
{
-
return DriverManager.getConnection("jdbc:postgresql://dbteach/", "ug73dxs", "thowrisw");
-
}
-
}
-
-
Why r resultsets not providing the same results as when i try them through direct code in main???
Thanks...
What did you say is the problem now?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Richard Cavell |
last post by:
Hi,
Using GCC on my G4, if I have a calculation like this:
#include <stdint.h>
uint64_t a = 0xffff * 0xffff ;
the result will be clobbered to 32 bits because that's the length of an
integer. How do I keep all the intermediate steps at 64 bits without
|
by: Denis Petronenko |
last post by:
Hello,
in the following code i have segmentaion fault instead of exception.
Why? What i must to do to catch exceptions in such situation? Used
compiler: gcc version 3.3.6 (Debian 1:3.3.6-13)
int main()
{
try{
int* p = NULL;
*p = 4;
|
by: Alexander Behnke |
last post by:
Hello,
I am currently facing a problem with a calloc function call which
returns a NULL pointer while in a debugging environment. If I start the
same executable from the shell (not via the debugger) a valid pointer is
returned.
The call stack looks like this:
FlowPRESS.exe!strcat() Line 169 Asm
|
by: dev24 |
last post by:
Hi all,
I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another method and display the result as an arraylist. The method and the object are as below:
public static ArrayList getProjects(int userID)
throws SQLException, Exception
{
ArrayList arrayList = null;
...
|
by: Sri |
last post by:
All
Recenetly our shop migrated to DB2 V8 from V7. We are in IBM System Level:
z/OS 1.6.1 @ RSU 0702.
Processor : IBM 2064-1C7 (z/900) # 1B89 Mode: 64-bit
One of my application is facing problem while testing their code/Jobs in DB2
V8 environments. The Details are: Job is abending with SQLCODE -302 SQL
error on the prepare staement. There is no code modification made for many
years. It is working fine in db2 V7 enviornments. The...
| |
by: bevis |
last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you can.
I'm using Eclipse 3.2.2 and I have installed mysql-connector-java-5.0.6-bin.jar. I am trying to connect to SQL Enterprise Manager version 8.0 innstall on a Windows 2003 Server Enterprise Edition.
I have been able to successfully test...
|
by: valentin tihomirov |
last post by:
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca ,
the pointers are allowed to be null, while references must refer an existing
variable of required type. The null is normally used for making optional
parameters. But there is no way to pass null reference in C#. Something is
missing.
|
by: MRamaLakshmi |
last post by:
hi,
I am developing an application using Java Applet which will be uploading files.
Its throwing Null Pointer exception while detecting the proxy when we are trying to load the applet using Java6.
It is working fine for Java5.
Here i am giving the piece of code where it is throwing me exception
protected void autoDetectProxy(URL url)
{
// Trying Java system variables.
|
by: ajos |
last post by:
hi evrybdy,
the problem is:-
i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem...
when i enter values in the 2 text boxes and click enter an exception occours which is-
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |