By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,237 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,237 IT Pros & Developers. It's quick & easy.

Jsp connection to postgresql database

P: 9
I am doing a project where i need to use a jsp to connect to a postgresql database and then show the results of a querry in the jsp. please can any1telme the steps and the jsp code ...thanks
Apr 4 '13 #1
Share this Question
Share on Google+
13 Replies


10K+
P: 13,264
Don't put the database connection code in a JSP. Put it in a normal java class and test that it works properly. Then call that Java class from the JSP when you need to display the results. That way you separate your processing logic from your presentation logic allowing for testable, reusable code which is also easy to read.
Apr 4 '13 #2

P: 9
hey thanx for the idea but can write the codes for it .. i hav been getting a lot of errors ...wil b very helpful
Apr 4 '13 #3

P: 9
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2. 2
  3. 3    
  4. 4
  5. 5     public class Connect {
  6. 6
  7. 7         public static void main(String argv[]) throws Exception {
  8. 8
  9. 9             // Initialize variables.
  10. 10            Connection con = null;
  11. 11
  12. 12            try {
  13. 13
  14. 14                // Set the connection URL.
  15. 15                String url = "jdbc:postgresql://localhost:5432/source_db"; // db name:  source_db
  16. 16
  17. 17                Class.forName("org.postgresql.Driver").newInstance();
  18. 18
  19. 19                // Connect.
  20. 20                con = DriverManager.getConnection(url);
  21. 21
  22.  
  23. 22               // Report on the results.
  24. 23               if (con != null) {
  25. 24                   System.out.println("A database connection has been established!");
  26. 25               }
  27. 26
  28. 27           } catch (Exception e) {
  29. 28
  30. 29               System.out.println("Problem: " + e.toString());
  31. 30
  32. 31           }
  33. 32
  34. 33           // Clean up.
  35. 34           finally {
  36. 35
  37. 36              if (con != null) {
  38. 37
  39. 38                  try {
  40. 39                      con.close();
  41. 40                  } catch (Exception e) {
  42. 41                      System.out.println(e.toString());
  43. 42                  }
  44. 43                  con = null;
  45. 44              }
  46. 45          }
// i wrote this code ....can u correct me??
Apr 4 '13 #4

10K+
P: 13,264
Use code tags when posting code and don't copy it here with line numbers.
What are the errors that you are getting?
Apr 4 '13 #5

P: 9
i think the connection is not established ..then i made class where i tried to establish a connection to database nly....it is getting compiled properly but not able to run...
the code :



Expand|Select|Wrap|Line Numbers
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.util.Properties;
  5.  
  6. public class xyz {
  7.     public static void main(String[] args) {
  8.  
  9.         Connection conn = null;
  10.  
  11.         try {
  12.  
  13.             String url = "jdbc:postgresql://localhost:5432/source_db";
  14.             String user = "postgres";
  15.             String password = "NIC@123";
  16.  
  17.             conn1 = DriverManager.getConnection(url, user, password);
  18.             if (conn1 != null) {
  19.                 System.out.println("Connected to the database postgres");
  20.             }
  21.  
  22.             }
  23.          catch (SQLException ex) {
  24.             System.out.println("An error occurred. Maybe user/password is invalid");
  25.             ex.printStackTrace();
  26.         }
  27.     }
  28. }
Apr 4 '13 #6

10K+
P: 13,264
Again, use code tags and again read the exception message. What does it say?
Apr 4 '13 #7

P: 9
it is now working .... now can u helo me in writing the code for using the connection class (xyz) to fetch querry results from database???
Apr 5 '13 #8

P: 9
but when do i use the class i made ?? no import statement??
Apr 5 '13 #10

P: 9
actually i am a little short on time ...so if u can give the correct code using my class to fetch a 'select * from dir_s' statement...it would be really helpful ... actually I am bit new to java and jsps.
Apr 5 '13 #11

10K+
P: 13,264
You would have been done reading that page by now and be able to do it yourself.
Apr 5 '13 #12

P: 9
well i read your doc n it ws very useful just wanted to ask u one thing ....
'public static void viewTable(Connection con, String dbName)
throws SQLException ' in ur doc is the connection class ???
Apr 5 '13 #13

P: 9
i have understood the steps aftr 'creating statement'
Apr 5 '13 #14

Post your reply

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