Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Connecting web application form with simple mysql command  RSS feed

 
Farshan Airy Airan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

Anyone here know how to insert and update record using simple prepared statement command (i'm using mysql). To insert record in jsp webform. I know there's other way, but it looks bit complicated to me (servlet,javabean,etc). The example code something similar down here :

<%
Class.forName("com.mysql.jdbc.Driver");

Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/test","root","bbb213");
Statement Stmt = Conn.createStatement();

ResultSet resultset= Stmt.executeQuery
("SELECT * FROM student" WHERE StudID="?);

%>

<BR><table width="500" border="1" align="center" bgcolor="#99CCCC">
<tr bgcolor="#FFFFCC">
<td width="165"><strong>Student Info</strong></td>
<td width="8"> </td>
<td width="310"> </td>
</tr>
<tr>
<td>Student Name:</td>
<td>:</td>
</tr>
<tr>
<td>Student ID:</td>
<td>:</td>
</tr>


Pls, i need a simple jsp that can insert and update record properly.Thanks in advance.
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP is a view technology - you should not put any database / servlet code in it. This is how I would do it.

1. Create a new Servlet.



2. In your web.xml map the above servlet to the url "/processStudent" or something like that.

3. This is how your jsp should look like
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Database access code should not be in your servlet either. Fabricate a seperate set of classes to handle db related stuff. you can google for DAO pattern.

For prototyping stuff JSTL SQL tags is your best bet.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!