| | |
How to Connect MySQL from JSP Page
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread 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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jan 2008
Posts: 36
Reputation:
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>•
•
•
•
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
JSP Syntax (Toggle Plain Text)
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: 36
Reputation:
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
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
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: Unable to connect to tomcat through browser
- Next Thread: MVC help
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






