473,802 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Null Pointer Exception for simple jdbc code??

8 New Member
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
Mar 1 '07 #1
6 4197
r035198x
13,262 MVP
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 ?
Mar 1 '07 #2
dev24
8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);  
  3.  
  4.  
If it helps the complete code is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.io.OutputStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import database.Users;
  12. import database.Projects;
  13. import database.Diagram;
  14. import JavaSVN.ExtractText;
  15. import java.sql.Statement;
  16.  
  17. public class QuerySourceData 
  18. {
  19.  
  20.     private static Connection con;  
  21.     private static Object connectionLock = new Object();
  22.  
  23.     private static PreparedStatement getProjectsStmt;
  24.     private static PreparedStatement getNameStmt;
  25.     private static PreparedStatement addUserStmt;
  26.     private static PreparedStatement addProjectStmt;
  27.     private static PreparedStatement addDiagramStmt;
  28.     private static PreparedStatement getfilePathStmt;
  29.  
  30.     private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
  31.     private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
  32.     private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
  33.     private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
  34.     private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
  35.     private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
  36.  
  37.     public QuerySourceData() {}
  38.  
  39.     private static void prepareQueries()
  40.         throws SQLException, Exception
  41.     {
  42.  
  43.         getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);            // gets all info about projects in which user is involved from userID
  44.  
  45.         getNameStmt = con.prepareStatement(GET_NAME_STMT);                  
  46.  
  47.     } 
  48.  
  49.     //enables connection to the database to perform the queries and updates
  50.     public static void connect()
  51.         throws SQLException, Exception
  52.     {
  53.         synchronized (connectionLock) 
  54.         {
  55.             if (con != null) 
  56.             {
  57.                 throw new IllegalStateException("Already connected to database, close first, then re-connect");
  58.         }
  59.  
  60.             try
  61.             {
  62.                 Properties props = new Properties();
  63.                 FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
  64.                 props.load(in);
  65.                 String driver = props.getProperty("jdbc.driver");
  66.                 Class.forName(driver);
  67.                 String url = props.getProperty("jdbc.url");
  68.                 String username = props.getProperty("jdbc.username");
  69.                 String password = props.getProperty("jdbc.password");       
  70.                 in.close();
  71.             }
  72.  
  73.             catch(Exception e)
  74.             {
  75.                 System.err.println("Exception: " + e);
  76.             }
  77.  
  78.         }
  79.  
  80.     }
  81.  
  82.     // determines if it is still connected to the database - designed for expansion
  83.     public static boolean isConnected() 
  84.     {
  85.         synchronized (connectionLock) 
  86.         {
  87.             return con != null;
  88.         }
  89.     }
  90.  
  91.     // closes connection to the database - designed for expansion
  92.     public static void close() 
  93.         throws SQLException, Exception
  94.     {
  95.         synchronized (connectionLock) 
  96.         {
  97.             con.close();
  98.             con = null;
  99.         }        
  100.     }
  101.  
  102.     public static void addUser(Users u) 
  103.         throws SQLException
  104.     {
  105.  
  106.         synchronized (connectionLock) 
  107.         {
  108.             try 
  109.             {
  110.                 addUserStmt.setInt(1, u.getuserID());
  111.                 addUserStmt.setString(2, u.getTitle());
  112.                 addUserStmt.setString(3, u.getfirstName());
  113.                 addUserStmt.setString(4, u.getlastName());
  114.                 addUserStmt.setString(5, u.getEmail());
  115.                 addUserStmt.setBoolean(6, u.getAdmin());
  116.         addUserStmt.setBoolean(7, u.getcreateProject());
  117.  
  118.         addUserStmt.executeUpdate();    
  119.             }
  120.  
  121.             catch (SQLException e) 
  122.             {
  123.                 throw e;
  124.             }
  125.         }
  126.     }
  127.  
  128.     public static void addProject(Projects p) 
  129.         throws SQLException
  130.     {
  131.  
  132.         synchronized (connectionLock) 
  133.         {
  134.             try 
  135.             {
  136.                 addProjectStmt.setInt(1, p.getprojectID());
  137.                 addProjectStmt.setInt(2, p.getuserID());
  138.                 addProjectStmt.setString(3, p.getprojectName());
  139.                 addProjectStmt.setString(4, p.getDescription());
  140.                 addProjectStmt.setInt(5, p.getcreateDate());
  141.                 addProjectStmt.setBoolean(6, p.getVisibility());
  142.  
  143.         addProjectStmt.executeUpdate();    
  144.             }
  145.  
  146.             catch (SQLException e) 
  147.             {
  148.                 throw e;
  149.             }
  150.         }
  151.     }
  152.  
  153.     public static void addDiagram(Diagram d) 
  154.         throws SQLException
  155.     {
  156.  
  157.         synchronized (connectionLock) 
  158.         {
  159.             try 
  160.             {
  161.                 addDiagramStmt.setInt(1, d.getdiagramID());
  162.                 addDiagramStmt.setInt(2, d.getuserID());
  163.                 addDiagramStmt.setInt(3, d.getprojectID());
  164.                 addDiagramStmt.setString(4, d.getdiagramName());
  165.                 addDiagramStmt.setString(5, d.getDescription());
  166.                 addDiagramStmt.setString(6, d.getSummary());
  167.         addDiagramStmt.setString(7, d.getfilePath());
  168.                 addDiagramStmt.setString(8, d.getdiagramUrl());
  169.                 addDiagramStmt.setInt(9, d.getcreateDate());
  170.  
  171.         addDiagramStmt.executeUpdate();    
  172.             }
  173.  
  174.             catch (SQLException e) 
  175.             {
  176.                 throw e;
  177.             }
  178.         }
  179.     }
  180.  
  181.     // gets names and IDs of projects in which user is involved from userID
  182.     public static ArrayList getProjects(int userID) 
  183.         throws SQLException, Exception 
  184.     {
  185.         ArrayList arrayList = null;
  186.         getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
  187.         getProjectsStmt.setInt(1, userID);
  188.         ResultSet rs = getProjectsStmt.executeQuery();
  189.         arrayList = new ArrayList();
  190.  
  191.         try
  192.         {
  193.             while (rs.next()) 
  194.             {
  195.                 Projects p = createProject(rs);
  196.                 arrayList.add(p);
  197.                 rs.close();
  198.  
  199.             }
  200.         }
  201.         catch(Exception e) 
  202.         {
  203.             System.err.println(e + ": UserID entered has not been found.");
  204.         }
  205.  
  206.         return arrayList;
  207.  
  208.     }
  209.  
  210.  
  211.     private static Projects createProject(ResultSet rs) 
  212.         throws SQLException, Exception 
  213.     {
  214.  
  215.         int projectID = rs.getInt("projectID");
  216.         int userID = rs.getInt("userID");
  217.         String name = rs.getString("name");
  218.         String description = rs.getString("description");
  219.         int createdate = rs.getInt("createdate");
  220.         boolean visibility = rs.getBoolean("visibility");
  221.  
  222.         return new Projects(projectID, userID, name, description, createdate, visibility);
  223.     }
  224.  
  225.     private static Diagram createDiagram(ResultSet rs) 
  226.         throws SQLException, Exception 
  227.     {
  228.  
  229.         int diagramID = rs.getInt("diagramID");
  230.         int userID = rs.getInt("userID");
  231.         int projectID = rs.getInt("projectID");
  232.         String name = rs.getString("name");
  233.         String description = rs.getString("description");
  234.         String summary = rs.getString("summary");
  235.         String filePath = rs.getString("filePath");
  236.         String diagramUrl = rs.getString("diagramUrl");
  237.         int createdate = rs.getInt("createdate");
  238.  
  239.         return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
  240.     }
  241.  
  242.     private static Users createUsers(ResultSet rs) 
  243.         throws SQLException, Exception 
  244.     {
  245.  
  246.         int userID = rs.getInt("userID");
  247.         String title = rs.getString("title");
  248.         String firstname = rs.getString("firstname");
  249.         String lastname = rs.getString("lastname");
  250.         String email = rs.getString("email");
  251.         boolean admin = rs.getBoolean("admin");
  252.         boolean createProject = rs.getBoolean("createProject");
  253.  
  254.         return new Users(userID, title, firstname, lastname, email, admin, createProject);
  255.     }  
  256.  
  257.     public static void main(String[] args)    
  258.         throws SQLException, Exception
  259.     { 
  260.  
  261.         connect();    
  262.         prepareQueries();
  263.         getProjects(1);
  264.  
  265.     }
  266. }
  267.  
Thanks!!

Dev
Mar 1 '07 #3
r035198x
13,262 MVP
Thanks for reply.. Sorry I'll bear that in mind... Basically there is a method prepareQueries( ) where I initialise getProjectsStmt as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT); 
  3.  
  4.  
If it helps the complete code is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.net.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.io.OutputStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import database.Users;
  12. import database.Projects;
  13. import database.Diagram;
  14. import JavaSVN.ExtractText;
  15. import java.sql.Statement;
  16.  
  17. public class QuerySourceData 
  18. {
  19.  
  20. private static Connection con; 
  21. private static Object connectionLock = new Object();
  22.  
  23. private static PreparedStatement getProjectsStmt;
  24. private static PreparedStatement getNameStmt;
  25. private static PreparedStatement addUserStmt;
  26. private static PreparedStatement addProjectStmt;
  27. private static PreparedStatement addDiagramStmt;
  28. private static PreparedStatement getfilePathStmt;
  29.  
  30. private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
  31. private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
  32. private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
  33. private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
  34. private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
  35. private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
  36.  
  37. public QuerySourceData() {}
  38.  
  39. private static void prepareQueries()
  40. throws SQLException, Exception
  41. {
  42.  
  43. getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
  44.  
  45. getNameStmt = con.prepareStatement(GET_NAME_STMT); 
  46.  
  47.  
  48. //enables connection to the database to perform the queries and updates
  49. public static void connect()
  50. throws SQLException, Exception
  51. {
  52. synchronized (connectionLock) 
  53. {
  54. if (con != null) 
  55. {
  56. throw new IllegalStateException("Already connected to database, close first, then re-connect");
  57.      }
  58.  
  59. try
  60. {
  61. Properties props = new Properties();
  62. FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
  63. props.load(in);
  64. String driver = props.getProperty("jdbc.driver");
  65. Class.forName(driver);
  66. String url = props.getProperty("jdbc.url");
  67. String username = props.getProperty("jdbc.username");
  68. String password = props.getProperty("jdbc.password"); 
  69. in.close();
  70. }
  71.  
  72. catch(Exception e)
  73. {
  74. System.err.println("Exception: " + e);
  75. }
  76.  
  77. }
  78.  
  79. }
  80.  
  81. // determines if it is still connected to the database - designed for expansion
  82. public static boolean isConnected() 
  83. {
  84. synchronized (connectionLock) 
  85. {
  86. return con != null;
  87. }
  88. }
  89.  
  90. // closes connection to the database - designed for expansion
  91. public static void close() 
  92. throws SQLException, Exception
  93. {
  94. synchronized (connectionLock) 
  95. {
  96. con.close();
  97. con = null;
  98. }
  99.  
  100. public static void addUser(Users u) 
  101. throws SQLException
  102. {
  103.  
  104. synchronized (connectionLock) 
  105. {
  106. try 
  107. {
  108. addUserStmt.setInt(1, u.getuserID());
  109. addUserStmt.setString(2, u.getTitle());
  110. addUserStmt.setString(3, u.getfirstName());
  111. addUserStmt.setString(4, u.getlastName());
  112. addUserStmt.setString(5, u.getEmail());
  113. addUserStmt.setBoolean(6, u.getAdmin());
  114.         addUserStmt.setBoolean(7, u.getcreateProject());
  115.  
  116.         addUserStmt.executeUpdate();    
  117. }
  118.  
  119. catch (SQLException e) 
  120. {
  121. throw e;
  122. }
  123. }
  124. }
  125.  
  126. public static void addProject(Projects p) 
  127. throws SQLException
  128. {
  129.  
  130. synchronized (connectionLock) 
  131. {
  132. try 
  133. {
  134. addProjectStmt.setInt(1, p.getprojectID());
  135. addProjectStmt.setInt(2, p.getuserID());
  136. addProjectStmt.setString(3, p.getprojectName());
  137. addProjectStmt.setString(4, p.getDescription());
  138. addProjectStmt.setInt(5, p.getcreateDate());
  139. addProjectStmt.setBoolean(6, p.getVisibility());
  140.  
  141.         addProjectStmt.executeUpdate();    
  142. }
  143.  
  144. catch (SQLException e) 
  145. {
  146. throw e;
  147. }
  148. }
  149. }
  150.  
  151. public static void addDiagram(Diagram d) 
  152. throws SQLException
  153. {
  154.  
  155. synchronized (connectionLock) 
  156. {
  157. try 
  158. {
  159. addDiagramStmt.setInt(1, d.getdiagramID());
  160. addDiagramStmt.setInt(2, d.getuserID());
  161. addDiagramStmt.setInt(3, d.getprojectID());
  162. addDiagramStmt.setString(4, d.getdiagramName());
  163. addDiagramStmt.setString(5, d.getDescription());
  164. addDiagramStmt.setString(6, d.getSummary());
  165.         addDiagramStmt.setString(7, d.getfilePath());
  166. addDiagramStmt.setString(8, d.getdiagramUrl());
  167. addDiagramStmt.setInt(9, d.getcreateDate());
  168.  
  169.         addDiagramStmt.executeUpdate();    
  170. }
  171.  
  172. catch (SQLException e) 
  173. {
  174. throw e;
  175. }
  176. }
  177. }
  178.  
  179. // gets names and IDs of projects in which user is involved from userID
  180. public static ArrayList getProjects(int userID) 
  181.     throws SQLException, Exception 
  182. {
  183. ArrayList arrayList = null;
  184. getProjectsStmt = con.prepareStatement(GET_PROJECTS_STMT);
  185. getProjectsStmt.setInt(1, userID);
  186. ResultSet rs = getProjectsStmt.executeQuery();
  187. arrayList = new ArrayList();
  188.  
  189. try
  190. {
  191. while (rs.next()) 
  192. {
  193. Projects p = createProject(rs);
  194. arrayList.add(p);
  195. rs.close();
  196.  
  197. }
  198. }
  199. catch(Exception e) 
  200. {
  201. System.err.println(e + ": UserID entered has not been found.");
  202. }
  203.  
  204. return arrayList;
  205.  
  206. }
  207.  
  208.  
  209. private static Projects createProject(ResultSet rs) 
  210. throws SQLException, Exception 
  211. {
  212.  
  213. int projectID = rs.getInt("projectID");
  214. int userID = rs.getInt("userID");
  215. String name = rs.getString("name");
  216. String description = rs.getString("description");
  217. int createdate = rs.getInt("createdate");
  218. boolean visibility = rs.getBoolean("visibility");
  219.  
  220. return new Projects(projectID, userID, name, description, createdate, visibility);
  221. }
  222.  
  223. private static Diagram createDiagram(ResultSet rs) 
  224. throws SQLException, Exception 
  225. {
  226.  
  227. int diagramID = rs.getInt("diagramID");
  228. int userID = rs.getInt("userID");
  229. int projectID = rs.getInt("projectID");
  230. String name = rs.getString("name");
  231. String description = rs.getString("description");
  232. String summary = rs.getString("summary");
  233. String filePath = rs.getString("filePath");
  234. String diagramUrl = rs.getString("diagramUrl");
  235. int createdate = rs.getInt("createdate");
  236.  
  237. return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
  238. }
  239.  
  240. private static Users createUsers(ResultSet rs) 
  241. throws SQLException, Exception 
  242. {
  243.  
  244. int userID = rs.getInt("userID");
  245. String title = rs.getString("title");
  246. String firstname = rs.getString("firstname");
  247. String lastname = rs.getString("lastname");
  248. String email = rs.getString("email");
  249. boolean admin = rs.getBoolean("admin");
  250. boolean createProject = rs.getBoolean("createProject");
  251.  
  252. return new Users(userID, title, firstname, lastname, email, admin, createProject);
  253.  
  254. public static void main(String[] args) 
  255. throws SQLException, Exception
  256.  
  257. connect(); 
  258. prepareQueries();
  259. getProjects(1);
  260.  
  261. }
  262. }
  263.  
Thanks!!

Dev
I have to go but look through your code and see how you intialized con

Expand|Select|Wrap|Line Numbers
  1.  private static Connection con; 
Your constructor does not initialize it either so it's probably null when you want to use it
Mar 1 '07 #4
dev24
8 New Member
I have to go but look through your code and see how you intialized con

Expand|Select|Wrap|Line Numbers
  1.  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
Mar 1 '07 #5
dev24
8 New Member
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. package database;
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.io.OutputStream;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import database.Users;
  14. import database.Project;
  15. import database.Diagram;
  16. import JavaSVN.ExtractText;
  17. import java.sql.Statement;
  18.  
  19.  
  20. public class QuerySourceData 
  21. {
  22.     private ExtractText ET;
  23.  
  24.  
  25.     private static Object connectionLock = new Object();
  26.  
  27.     private static PreparedStatement getProjectsStmt;
  28.     private static PreparedStatement getNameStmt;
  29.     private static PreparedStatement addUserStmt;
  30.     private static PreparedStatement addProjectStmt;
  31.     private static PreparedStatement addDiagramStmt;
  32.     private static PreparedStatement getfilePathStmt;
  33.  
  34.     private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
  35.     private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
  36.     private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
  37.  
  38.     private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
  39.     private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
  40.     private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
  41.  
  42.  
  43.     public QuerySourceData() 
  44.         throws Exception
  45.     {
  46.         try
  47.         {
  48.             Properties props = new Properties();
  49.             FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
  50.             props.load(in);
  51.             String driver = props.getProperty("jdbc.driver");
  52.             Class.forName(driver);
  53.             String url = props.getProperty("jdbc.url");
  54.             String username = props.getProperty("jdbc.username");
  55.             String password = props.getProperty("jdbc.password");       
  56.             in.close();
  57.         }
  58.  
  59.         catch(Exception e)
  60.         {
  61.             System.err.println("Exception: " + e);
  62.         }
  63.  
  64.         try
  65.         {
  66.             Connection conn = QuerySourceData.getConnection();
  67.  
  68.             getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT);            // gets all info about projects in which user is involved from userID
  69.             getNameStmt = conn.prepareStatement(GET_NAME_STMT);                  //gets names of diagrams from projectID
  70.             getfilePathStmt = conn.prepareStatement(GET_FILEPATH_STMT);
  71.         }
  72.         catch(Exception e)
  73.         {
  74.             System.out.println(e);
  75.         }
  76.     }
  77.  
  78.  
  79.  
  80.     public static void addUser(Users u) 
  81.         throws SQLException
  82.     {
  83.  
  84.         synchronized (connectionLock) 
  85.         {
  86.             try 
  87.             {
  88.                 addUserStmt.setInt(1, u.getuserID());
  89.                 addUserStmt.setString(2, u.getTitle());
  90.                 addUserStmt.setString(3, u.getfirstName());
  91.                 addUserStmt.setString(4, u.getlastName());
  92.                 addUserStmt.setString(5, u.getEmail());
  93.                 addUserStmt.setBoolean(6, u.getAdmin());
  94.         addUserStmt.setBoolean(7, u.getcreateProject());
  95.  
  96.         addUserStmt.executeUpdate();    
  97.             }
  98.  
  99.             catch (SQLException e) 
  100.             {
  101.                 throw e;
  102.             }
  103.         }
  104.     }
  105.  
  106.     public static void addProject(Project p) 
  107.         throws SQLException
  108.     {
  109.  
  110.         synchronized (connectionLock) 
  111.         {
  112.             try 
  113.             {
  114.                 addProjectStmt.setInt(1, p.getprojectID());
  115.                 addProjectStmt.setInt(2, p.getuserID());
  116.                 addProjectStmt.setString(3, p.getprojectName());
  117.                 addProjectStmt.setString(4, p.getDescription());
  118.                 addProjectStmt.setInt(5, p.getcreateDate());
  119.                 addProjectStmt.setBoolean(6, p.getVisibility());
  120.  
  121.         addProjectStmt.executeUpdate();    
  122.             }
  123.  
  124.             catch (SQLException e) 
  125.             {
  126.                 throw e;
  127.             }
  128.         }
  129.     }
  130.  
  131.     public static void addDiagram(Diagram d) 
  132.         throws SQLException
  133.     {
  134.  
  135.         synchronized (connectionLock) 
  136.         {
  137.             try 
  138.             {
  139.                 addDiagramStmt.setInt(1, d.getdiagramID());
  140.                 addDiagramStmt.setInt(2, d.getuserID());
  141.                 addDiagramStmt.setInt(3, d.getprojectID());
  142.                 addDiagramStmt.setString(4, d.getdiagramName());
  143.                 addDiagramStmt.setString(5, d.getDescription());
  144.                 addDiagramStmt.setString(6, d.getSummary());
  145.         addDiagramStmt.setString(7, d.getfilePath());
  146.                 addDiagramStmt.setString(8, d.getdiagramUrl());
  147.                 addDiagramStmt.setInt(9, d.getcreateDate());
  148.  
  149.         addDiagramStmt.executeUpdate();    
  150.             }
  151.  
  152.             catch (SQLException e) 
  153.             {
  154.                 throw e;
  155.             }
  156.         }
  157.     }
  158.  
  159.  
  160.  
  161.      public static String getDiagrams(int projectID) 
  162.         throws SQLException, Exception 
  163.     {
  164.         new QuerySourceData();
  165.         Connection conn = QuerySourceData.getConnection();
  166.         getNameStmt = conn.prepareStatement(GET_NAME_STMT);
  167.         getNameStmt.setInt(1, projectID);
  168.         ResultSet rs = getNameStmt.executeQuery();
  169.  
  170.         if(rs.first())
  171.         {
  172.             Diagram d = createDiagram(rs);              
  173.             rs.close();
  174.             conn.close();
  175.             String a = d.toString(); 
  176.             return a;
  177.         }
  178.         else 
  179.         {
  180.             throw new Exception("An error occured!");
  181.         }
  182.  
  183.     }
  184.  
  185.     // gets names and IDs of projects in which user is involved from userID
  186.     public static void getProjects(int userID) 
  187.         throws SQLException, Exception 
  188.     {
  189.         new QuerySourceData();
  190.         Connection conn = QuerySourceData.getConnection();            
  191.         getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT); 
  192.         getProjectsStmt.setInt(1, userID);
  193.         ResultSet rs = getProjectsStmt.executeQuery();
  194.  
  195.         if(rs.first()) 
  196.         {
  197.             Project p = createProject(rs);
  198.             rs.close();
  199.             conn.close();
  200.             String a = p.toString();
  201.             System.out.println(a);
  202.             //return a;
  203.         }
  204.  
  205.          else
  206.          {
  207.             throw new Exception("An error occured");
  208.          }    
  209.     }
  210.  
  211.     private static Project createProject(ResultSet rs) 
  212.         throws SQLException, Exception 
  213.     {
  214.  
  215.         int projectID = rs.getInt("projectID");
  216.         int userID = rs.getInt("userID");
  217.         String name = rs.getString("name");
  218.         String description = rs.getString("description");
  219.         int createdate = rs.getInt("createdate");
  220.         boolean visibility = rs.getBoolean("visibility");
  221.  
  222.         return new Project(projectID, userID, name, description, createdate, visibility);
  223.     }
  224.  
  225.     private static Diagram createDiagram(ResultSet rs) 
  226.         throws SQLException, Exception 
  227.     {
  228.  
  229.         int diagramID = rs.getInt("diagramID");
  230.         int userID = rs.getInt("userID");
  231.         int projectID = rs.getInt("projectID");
  232.         String name = rs.getString("name");
  233.         String description = rs.getString("description");
  234.         String summary = rs.getString("summary");
  235.         String filePath = rs.getString("filePath");
  236.         String diagramUrl = rs.getString("diagramUrl");
  237.         int createdate = rs.getInt("createdate");
  238.  
  239.         return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
  240.     }
  241.  
  242.     private static Users createUsers(ResultSet rs) 
  243.         throws SQLException, Exception 
  244.     {
  245.  
  246.         int userID = rs.getInt("userID");
  247.         String title = rs.getString("title");
  248.         String firstname = rs.getString("firstname");
  249.         String lastname = rs.getString("lastname");
  250.         String email = rs.getString("email");
  251.         boolean admin = rs.getBoolean("admin");
  252.         boolean createProject = rs.getBoolean("createProject");
  253.  
  254.         return new Users(userID, title, firstname, lastname, email, admin, createProject);
  255.     }  
  256.  
  257.     public static void main(String[] args)    
  258.         throws SQLException, Exception
  259.     { 
  260.  
  261.         getProjects(2);
  262.         getDiagrams(3);          
  263.     }
  264.  
  265.      public static Connection getConnection() 
  266.         throws SQLException
  267.     {
  268.         return DriverManager.getConnection("jdbc:postgresql://dbteach/", "ug73dxs", "thowrisw");
  269.     }
  270. }
  271.  
  272.  

Why r resultsets not providing the same results as when i try them through direct code in main???

Thanks...
Mar 2 '07 #6
r035198x
13,262 MVP
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. package database;
  3.  
  4. import java.net.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.io.OutputStream;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import database.Users;
  14. import database.Project;
  15. import database.Diagram;
  16. import JavaSVN.ExtractText;
  17. import java.sql.Statement;
  18.  
  19.  
  20. public class QuerySourceData 
  21. {
  22. private ExtractText ET;
  23.  
  24.  
  25. private static Object connectionLock = new Object();
  26.  
  27. private static PreparedStatement getProjectsStmt;
  28. private static PreparedStatement getNameStmt;
  29. private static PreparedStatement addUserStmt;
  30. private static PreparedStatement addProjectStmt;
  31. private static PreparedStatement addDiagramStmt;
  32. private static PreparedStatement getfilePathStmt;
  33.  
  34. private static final String GET_NAME_STMT = "SELECT diagramID, name FROM diagram WHERE projectID = ?";
  35. private static final String GET_PROJECTS_STMT = "SELECT projectID, name FROM project WHERE userID = ?";
  36. private static final String GET_FILEPATH_STMT = "SELECT filePath FROM diagram WHERE diagramID = ?";
  37.  
  38. private static final String ADD_USER_STMT = "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?)";
  39. private static final String ADD_PROJECT_STMT = "INSERT INTO Project VALUES (?, ?, ?, ?, ?, ?)";
  40. private static final String ADD_DIAGRAM_STMT = "INSERT INTO Diagram VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
  41.  
  42.  
  43. public QuerySourceData() 
  44. throws Exception
  45. {
  46. try
  47. {
  48. Properties props = new Properties();
  49. FileInputStream in = new FileInputStream("Z:\\Database.Properties.txt");
  50. props.load(in);
  51. String driver = props.getProperty("jdbc.driver");
  52. Class.forName(driver);
  53. String url = props.getProperty("jdbc.url");
  54. String username = props.getProperty("jdbc.username");
  55. String password = props.getProperty("jdbc.password"); 
  56. in.close();
  57. }
  58.  
  59. catch(Exception e)
  60. {
  61. System.err.println("Exception: " + e);
  62. }
  63.  
  64. try
  65. {
  66. Connection conn = QuerySourceData.getConnection();
  67.  
  68. getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT); // gets all info about projects in which user is involved from userID
  69. getNameStmt = conn.prepareStatement(GET_NAME_STMT); //gets names of diagrams from projectID
  70. getfilePathStmt = conn.prepareStatement(GET_FILEPATH_STMT);
  71. }
  72. catch(Exception e)
  73. {
  74. System.out.println(e);
  75. }
  76. }
  77.  
  78.  
  79.  
  80. public static void addUser(Users u) 
  81. throws SQLException
  82. {
  83.  
  84. synchronized (connectionLock) 
  85. {
  86. try 
  87. {
  88. addUserStmt.setInt(1, u.getuserID());
  89. addUserStmt.setString(2, u.getTitle());
  90. addUserStmt.setString(3, u.getfirstName());
  91. addUserStmt.setString(4, u.getlastName());
  92. addUserStmt.setString(5, u.getEmail());
  93. addUserStmt.setBoolean(6, u.getAdmin());
  94.         addUserStmt.setBoolean(7, u.getcreateProject());
  95.  
  96.         addUserStmt.executeUpdate();    
  97. }
  98.  
  99. catch (SQLException e) 
  100. {
  101. throw e;
  102. }
  103. }
  104. }
  105.  
  106. public static void addProject(Project p) 
  107. throws SQLException
  108. {
  109.  
  110. synchronized (connectionLock) 
  111. {
  112. try 
  113. {
  114. addProjectStmt.setInt(1, p.getprojectID());
  115. addProjectStmt.setInt(2, p.getuserID());
  116. addProjectStmt.setString(3, p.getprojectName());
  117. addProjectStmt.setString(4, p.getDescription());
  118. addProjectStmt.setInt(5, p.getcreateDate());
  119. addProjectStmt.setBoolean(6, p.getVisibility());
  120.  
  121.         addProjectStmt.executeUpdate();    
  122. }
  123.  
  124. catch (SQLException e) 
  125. {
  126. throw e;
  127. }
  128. }
  129. }
  130.  
  131. public static void addDiagram(Diagram d) 
  132. throws SQLException
  133. {
  134.  
  135. synchronized (connectionLock) 
  136. {
  137. try 
  138. {
  139. addDiagramStmt.setInt(1, d.getdiagramID());
  140. addDiagramStmt.setInt(2, d.getuserID());
  141. addDiagramStmt.setInt(3, d.getprojectID());
  142. addDiagramStmt.setString(4, d.getdiagramName());
  143. addDiagramStmt.setString(5, d.getDescription());
  144. addDiagramStmt.setString(6, d.getSummary());
  145.         addDiagramStmt.setString(7, d.getfilePath());
  146. addDiagramStmt.setString(8, d.getdiagramUrl());
  147. addDiagramStmt.setInt(9, d.getcreateDate());
  148.  
  149.         addDiagramStmt.executeUpdate();    
  150. }
  151.  
  152. catch (SQLException e) 
  153. {
  154. throw e;
  155. }
  156. }
  157. }
  158.  
  159.  
  160.  
  161. public static String getDiagrams(int projectID) 
  162.     throws SQLException, Exception 
  163. {
  164. new QuerySourceData();
  165. Connection conn = QuerySourceData.getConnection();
  166. getNameStmt = conn.prepareStatement(GET_NAME_STMT);
  167. getNameStmt.setInt(1, projectID);
  168. ResultSet rs = getNameStmt.executeQuery();
  169.  
  170. if(rs.first())
  171. {
  172. Diagram d = createDiagram(rs); 
  173. rs.close();
  174. conn.close();
  175. String a = d.toString(); 
  176. return a;
  177. }
  178. else 
  179. {
  180. throw new Exception("An error occured!");
  181. }
  182.  
  183. }
  184.  
  185. // gets names and IDs of projects in which user is involved from userID
  186. public static void getProjects(int userID) 
  187.     throws SQLException, Exception 
  188. {
  189. new QuerySourceData();
  190. Connection conn = QuerySourceData.getConnection(); 
  191. getProjectsStmt = conn.prepareStatement(GET_PROJECTS_STMT); 
  192. getProjectsStmt.setInt(1, userID);
  193. ResultSet rs = getProjectsStmt.executeQuery();
  194.  
  195. if(rs.first()) 
  196. {
  197. Project p = createProject(rs);
  198. rs.close();
  199. conn.close();
  200. String a = p.toString();
  201. System.out.println(a);
  202. //return a;
  203. }
  204.  
  205. else
  206. {
  207. throw new Exception("An error occured");
  208. }
  209.  
  210. private static Project createProject(ResultSet rs) 
  211. throws SQLException, Exception 
  212. {
  213.  
  214. int projectID = rs.getInt("projectID");
  215. int userID = rs.getInt("userID");
  216. String name = rs.getString("name");
  217. String description = rs.getString("description");
  218. int createdate = rs.getInt("createdate");
  219. boolean visibility = rs.getBoolean("visibility");
  220.  
  221. return new Project(projectID, userID, name, description, createdate, visibility);
  222. }
  223.  
  224. private static Diagram createDiagram(ResultSet rs) 
  225. throws SQLException, Exception 
  226. {
  227.  
  228. int diagramID = rs.getInt("diagramID");
  229. int userID = rs.getInt("userID");
  230. int projectID = rs.getInt("projectID");
  231. String name = rs.getString("name");
  232. String description = rs.getString("description");
  233. String summary = rs.getString("summary");
  234. String filePath = rs.getString("filePath");
  235. String diagramUrl = rs.getString("diagramUrl");
  236. int createdate = rs.getInt("createdate");
  237.  
  238. return new Diagram(diagramID, userID, projectID, name, description, summary, filePath, diagramUrl, createdate);
  239. }
  240.  
  241. private static Users createUsers(ResultSet rs) 
  242. throws SQLException, Exception 
  243. {
  244.  
  245. int userID = rs.getInt("userID");
  246. String title = rs.getString("title");
  247. String firstname = rs.getString("firstname");
  248. String lastname = rs.getString("lastname");
  249. String email = rs.getString("email");
  250. boolean admin = rs.getBoolean("admin");
  251. boolean createProject = rs.getBoolean("createProject");
  252.  
  253. return new Users(userID, title, firstname, lastname, email, admin, createProject);
  254.  
  255. public static void main(String[] args) 
  256. throws SQLException, Exception
  257.  
  258. getProjects(2);
  259. getDiagrams(3); 
  260. }
  261.  
  262. public static Connection getConnection() 
  263. throws SQLException
  264. {
  265. return DriverManager.getConnection("jdbc:postgresql://dbteach/", "ug73dxs", "thowrisw");
  266. }
  267. }
  268.  
  269.  

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?
Mar 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

14
2465
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
18
2981
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;
3
2118
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
1
5337
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; ...
22
6664
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...
2
4559
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...
76
4729
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.
1
3500
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.
1
3374
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.
0
9699
marktang
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...
0
9562
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,...
0
10538
Oralloy
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...
0
10305
jinu1996
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...
0
9115
agi2029
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...
1
7598
isladogs
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...
0
6838
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();...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.