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(rs);
arrayList.add(p);
rs.close();
}
}
catch(Exception e)
{
System.err.println(e);
}
return arrayList;
}
//Query is as follows
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
//createProject method that creates the Project object is as below
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);
}
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.NullPointerException
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 4171
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(rs);
arrayList.add(p);
rs.close();
}
}
catch(Exception e)
{
System.err.println(e);
}
return arrayList;
}
//Query is as follows
private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
//createProject method that creates the Project object is as below
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);
}
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.NullPointerException
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_STMT or getProjectsStmt?
1.Use code tags when posting code
2.Are you using GET_PROJECTS_STMT 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.println 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.println 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...
|
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)
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
| |