473,396 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Jsp connection to postgresql database

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
13 7329
r035198x
13,262 8TB
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
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
Again, use code tags and again read the exception message. What does it say?
Apr 4 '13 #7
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
but when do i use the class i made ?? no import statement??
Apr 5 '13 #10
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
r035198x
13,262 8TB
You would have been done reading that page by now and be able to do it yourself.
Apr 5 '13 #12
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
i have understood the steps aftr 'creating statement'
Apr 5 '13 #14

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

Similar topics

4
by: Ish Ahluwalia | last post by:
Hi: I'm very new to PostGreSql database and actually looking to evaluate for one of our project needs. Below please find some questions. I'm hoping if I can get some insight into these...
0
by: btober | last post by:
I noticed this gem for allowing the saving of MS-Project 2000 data into a Postgresql database, http://techdocs.postgresql.org/source.php#fmsproject posted by John P Cavanaugh probably a...
4
by: Digital Fart | last post by:
howto make a connection to database available in my classes. What is the best practice when i want to write classes that need a connection to the database? Do i make a conn variable in my...
2
by: Jeff Davis | last post by:
I have virtual hosted users on a server and some of them have a postgresql database. I'm concerned about the outside possibility that a user could create an infinite loop and fill up the partition...
0
by: vicky | last post by:
Hello Experts, Trying to run sample Postgrel's ECPG(Embedded SQL)programs on RHL 9.0. Im unable to connect to PostgreSQL database (sirishadb) when I run the program .... # su postgres...
1
by: somaskarthic | last post by:
Hi In postgresql database how to retrieve the date in a required format ? Is ther any method to retrieve the stored date value in mm/dd/yyyy format. I don't want to set the date format in...
6
by: codepirate | last post by:
Hi All, I am trying to connct to a serverless/embedded PostgreSQL database from a C# application using VS 2005. First of all, is this possible with PostgreSQL? I know this can be done with some...
1
by: bjvijayakumar | last post by:
I want to store some pdf files in the database, What would be the datatype for the same. If anyone knows please give me the details. I am very new to the PostgreSql database.
1
by: madhu542 | last post by:
Hi, I installed and configured postgresql-8.2.5, and server is also running fine in command line as well as in graphical mode (pgadmin3). I wrote a c-program to connect to the database...
0
by: David Michael Schruth, | last post by:
Hi, I am sort of in a jam here. I am using the PsycoPG2 library to read data out of a windows XP based PostGIS / PostGreSQL database but I am apparently unable to write (update or insert) even...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.