•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 401,628 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,952 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: 11334 | Replies: 33 | Solved
![]() |
Use a bean, as it is suppossed to be done.
You shouldn't have any scriptlets in your code anyway, so having to duplicate the code over and over again shouldn't be a problem in the first place.
You shouldn't have any scriptlets in your code anyway, so having to duplicate the code over and over again shouldn't be a problem in the first place.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,220
Reputation:
Rep Power: 10
Solved Threads: 270
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: Dec 2004
Location: London or Slovakia
Posts: 2,220
Reputation:
Rep Power: 10
Solved Threads: 270
Well I need to point you toward Announcements, that clearly state how this community work. So please read it befor next post
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: Jan 2008
Posts: 35
Reputation:
Rep Power: 1
Solved Threads: 4
Don't mix Servlet and JSP. Add the following code into a JSP page. Don't put it inside a function. JSP pages don't use function. Servlets do.
By the way. Database connections and business logic does not belong in the JSP pages. Move them down to Servlet or other specialized classes for the task.
By the way. Database connections and business logic does not belong in the JSP pages. Move them down to Servlet or other specialized classes for the task.
<html>
<head>
<title></title>
</head>
<body>
<%
try{
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8009/test", "root", "asma");
java.sql.PreparedStatement stmt = conn.prepareStatement("SELECT * FROM test");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
}
}
catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html> •
•
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 94
Reputation:
Rep Power: 2
Solved Threads: 6
•
•
•
•
First thing, please do not use JSP to connect to database. Pass data to servlet for validation, or use JavaScript to valildate them in JSP, and setup connection inside servlet and then by the needs go back to previous page to pick up correct/new data or move to nex page
this is usual peace of code which I re-use often
public class SomeClassName extends HttpServlet { Connection conn; Statement stmt; public void init() throws ServletException { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { System.out.println("Exception is : " + ex.toString() ); } } public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { conn = DriverManager.getConnection("jdbc:mysql://localhost/" + "database_name?user=my_username&password=my_password"); stmt = conn.createStatement(); } catch(SQLException ex) { System.out.println("SQLException : " + ex.getMessage() ); }
PS: I will request this post to be move to JSP section, it is where it actualy belong and where you can find more similar questions and answers
How do I implement this class in JSP page ?
Thanks,
Kusno.
NEVER NEVER NEVER GIVE UP
•
•
Join Date: Jan 2008
Posts: 35
Reputation:
Rep Power: 1
Solved Threads: 4
This class is the JSP. JSP is an extension of Servlet. Simply told. If you see a few pages up on this article you see how to create a connection in JSP. But as i already had sead. Don't use connection or other business related code inside JSP. It makes it more difficult to read Tags and Java code. Move the Connection and Query code down in a DAO layer and use Servlet class as controler. Put the data you want into a JavaBean and put it into Request or session. Get it back in the JSP page and view it. If you need help in how to implement it give me a reply
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,220
Reputation:
Rep Power: 10
Solved Threads: 270
This is a wrong approach!
JSP => presentation purpose
Servlet => logic execution
That mean servlet is in charge of database connection. Get your self up to date...
JSP => presentation purpose
Servlet => logic execution
That mean servlet is in charge of database connection. Get your self up to date...
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How to connect Oracle database from JSP page (Java)
- Very Very Urgent...Need Code for Calculating Execution Time For Jsp Page (JSP)
- Inserting uft8 strings into MySQL from jsp page (JSP)
- how to connect oracle 8i database with jsp page (JSP)
- Dynamic links in JSP page-need help (JSP)
- how to call javabean from jsp page upon onChange event (JSP)
- setting up MyODBC to connect to MySQL, need help.... (MySQL)
- I think I'm going to cry... (JSP)
Other Threads in the JSP Forum
- Previous Thread: jsp custom tag
- Next Thread: Travel database MySql,netbeans samples?



Linear Mode