•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 423,574 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 3,469 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: 543 | Replies: 7
![]() |
•
•
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 100
Reputation:
Rep Power: 2
Solved Threads: 7
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
So how do I set that connection in all JSP pages with same class ?
Thanks,
Kusno.
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
JSP Syntax (Toggle Plain Text)
import java.sql.*; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.swing.JOptionPane; public class Conn { public String PROP_FILE = "initial.ini"; public Conn() { } public Connection getConnection() throws SQLException { Connection cn = null; try { Class.forName("com.mysql.jdbc.Driver"); Properties p2 = new Properties(); p2 = loadProperties(PROP_FILE); String server; String database; String userID; String password; String url; server = p2.getProperty("Server"); database = p2.getProperty("Database"); userID = p2.getProperty("UserID"); password = p2.getProperty("Password"); url = "jdbc:mysql://" + server + "/" + database; cn = DriverManager.getConnection (url, userID, password); return cn; } catch (SQLException se) { System.out.println(se.toString()); return null; } catch (Exception ex) { System.out.println(ex.toString()); return null; } } public static Properties loadProperties(String sFile) { Properties p = new Properties(); try { FileInputStream in = new FileInputStream(sFile); p.load(in); in.close(); } catch (IOException iOException) { JOptionPane.showMessageDialog(null, iOException); } return p; } } in all GUI forms, I only set this code : void isiTable(String kataKunci) { String SQL = ""; SQL = "SELECT UserID, UserName, Status FROM masteruser "; tabMode.setRowCount(0); try { [b][color="Green"]Koneksi getCn = new Conn(); Connection cn = getCn.getConnection();[/color][/b] Statement st = cn.createStatement(); ResultSet rs = st.executeQuery(SQL); while(rs.next()) { String Code = rs.getString(1); String Name = rs.getString(2); String Status = rs.getString(3); String[] data = {Code,Name,Status}; tabMode.addRow(data); } } catch(SQLException e) { } }
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
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 197
•
•
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 100
Reputation:
Rep Power: 2
Solved Threads: 7
•
•
•
•
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
JSP Syntax (Toggle Plain Text)
Connection cn = null; try { Class.forName("com.mysql.jdbc.Driver"); String server; String database; String userID; String password; String url; server = "localhost:3306; database = "mydb"; userID = "root"; password = "justEnter"; url = "jdbc:mysql://" + server + "/" + database; cn = DriverManager.getConnection (url, userID, password); return cn; } catch (SQLException se) { System.out.println(se.toString()); return null; } catch (Exception ex) { System.out.println(ex.toString()); return null; }
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
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,433
Reputation:
Rep Power: 11
Solved Threads: 295
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.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 100
Reputation:
Rep Power: 2
Solved Threads: 7
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.
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
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,433
Reputation:
Rep Power: 11
Solved Threads: 295
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.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 197
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: JSP page calling a excel file that resides in TomCat
- Next Thread: Need Urgent HELP-Syntax error, insert "AssignmentOperator Expression" to complete Ass



Linear Mode