User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 402,699 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,361 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 483 | Replies: 7
Reply
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 94
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster in Training

Connection is recognised in all JSP files/pages

  #1  
Apr 30th, 2008
Dear all,
I'm newbie in Java/JSP. I have created project with Java Desktop for university lecture subject. But I still have one task, still with Java but in Web environment.
I have this class. This class is used to get connection to MySQL. I create it for Java GUI

  1. import java.sql.*;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.util.Properties;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class Conn
  8. {
  9. public String PROP_FILE = "initial.ini";
  10.  
  11. public Conn() {
  12. }
  13.  
  14. public Connection getConnection() throws SQLException {
  15. Connection cn = null;
  16. try {
  17. Class.forName("com.mysql.jdbc.Driver");
  18. Properties p2 = new Properties();
  19. p2 = loadProperties(PROP_FILE);
  20. String server;
  21. String database;
  22. String userID;
  23. String password;
  24. String url;
  25.  
  26. server = p2.getProperty("Server");
  27. database = p2.getProperty("Database");
  28. userID = p2.getProperty("UserID");
  29. password = p2.getProperty("Password");
  30.  
  31. url = "jdbc:mysql://" + server + "/" + database;
  32. cn = DriverManager.getConnection (url, userID, password);
  33. return cn;
  34. }
  35. catch (SQLException se)
  36. {
  37. System.out.println(se.toString());
  38. return null;
  39. } catch (Exception ex)
  40. {
  41. System.out.println(ex.toString());
  42. return null;
  43. }
  44. }
  45.  
  46. public static Properties loadProperties(String sFile) {
  47. Properties p = new Properties();
  48. try {
  49. FileInputStream in = new FileInputStream(sFile);
  50. p.load(in);
  51. in.close();
  52. } catch (IOException iOException) {
  53. JOptionPane.showMessageDialog(null, iOException);
  54. }
  55. return p;
  56. }
  57. }
  58.  
  59.  
  60. in all GUI forms, I only set this code :
  61.  
  62. void isiTable(String kataKunci)
  63. {
  64. String SQL = "";
  65. SQL = "SELECT UserID, UserName, Status FROM masteruser ";
  66. tabMode.setRowCount(0);
  67. try
  68. {
  69. [b][color="Green"]Koneksi getCn = new Conn();
  70. Connection cn = getCn.getConnection();[/color][/b]
  71. Statement st = cn.createStatement();
  72. ResultSet rs = st.executeQuery(SQL);
  73. while(rs.next())
  74. {
  75. String Code = rs.getString(1);
  76. String Name = rs.getString(2);
  77. String Status = rs.getString(3);
  78. String[] data = {Code,Name,Status};
  79. tabMode.addRow(data);
  80. }
  81. }
  82. catch(SQLException e)
  83. {
  84.  
  85. }
  86. }

So how do I set that connection in all JSP pages with same class ?

Thanks,

Kusno.
Last edited by peter_budo : Apr 30th, 2008 at 2:51 am. Reason: Keep It Organized - please use [code] tags
NEVER NEVER NEVER GIVE UP
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,700
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Connection is recognised in all JSP files/pages

  #2  
Apr 30th, 2008
You don't need any JDBC connections in JSP.
And you certainly don't want to keep a connection open past the lifetime of a single http request.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 94
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster in Training

Re: Connection is recognised in all JSP files/pages

  #3  
Apr 30th, 2008
Originally Posted by jwenting View Post
You don't need any JDBC connections in JSP.
And you certainly don't want to keep a connection open past the lifetime of a single http request.


I think I know what you mean.
But, I only wonder how I declare my class Conn in JSP page ?
So I don't have to write these code in every pages

  1. Connection cn = null;
  2. try {
  3. Class.forName("com.mysql.jdbc.Driver");
  4. String server;
  5. String database;
  6. String userID;
  7. String password;
  8. String url;
  9.  
  10. server = "localhost:3306;
  11. database = "mydb";
  12. userID = "root";
  13. password = "justEnter";
  14.  
  15. url = "jdbc:mysql://" + server + "/" + database;
  16. cn = DriverManager.getConnection (url, userID, password);
  17. return cn;
  18. }
  19. catch (SQLException se)
  20. {
  21. System.out.println(se.toString());
  22. return null;
  23. } catch (Exception ex)
  24. {
  25. System.out.println(ex.toString());
  26. return null;
  27. }
Last edited by peter_budo : Apr 30th, 2008 at 2:52 am. Reason: Keep It Organized - please use [code] tags
NEVER NEVER NEVER GIVE UP
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,232
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Connection is recognised in all JSP files/pages

  #4  
Apr 30th, 2008
Please, please, do not do any connection to database from JSP, it is bad thing. JSP is for presentation purposes, it is servlet that should be used for logic (on server validation, database communication)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 94
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster in Training

Re: Connection is recognised in all JSP files/pages

  #5  
Apr 30th, 2008
I read from some JSP books and their connection put it JSP page and I think it's not flexible.
So I'm still anxious if there is any solution to solve my question.

If Peter said, I should use servlet, do you have example or link for me to learn ?

Thank,

Kusno

NB : Sorry for my mistake by submitting this thread I don't use Toggle Plain Text for coding.
NEVER NEVER NEVER GIVE UP
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,232
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Connection is recognised in all JSP files/pages

  #6  
Apr 30th, 2008
Servlet connection to db examples, and something to read on Java and web development The Java EE 5 Tutorial, I'm sure that jwenting know some other resources that are good for java beginners
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,700
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Connection is recognised in all JSP files/pages

  #7  
Apr 30th, 2008
For beginners, the best advice is to stay away from enterprise technologies until they have the basics down solid.

For those who insist, Oracle has a large number of tutorials on their OTN website. They're however closely linked to their own JDeveloper product, just like most new Sun tutorials are closely linked to Netbeans.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Connection is recognised in all JSP files/pages

  #8  
May 1st, 2008
No matter what kind of tutorials you read, learning from a good book is almost always irreplaceable. For JSP/Servlets, Head First Servlets and JSP is a good choice IMO.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JSP Forum

All times are GMT -4. The time now is 5:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC