•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 455,973 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,819 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: 2492 | Replies: 2
![]() |
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I am using a JSP from to collect data from user and then inserting those values into a table in MySQL.But it is returning 'null' always.I am able to query the DB and view the entire table and everything else is fine. But the INSERT is not working somehow!!!
Please help me in solving my problem!!!
The code is as follows:
Thank You!!!
I am using a JSP from to collect data from user and then inserting those values into a table in MySQL.But it is returning 'null' always.I am able to query the DB and view the entire table and everything else is fine. But the INSERT is not working somehow!!!
Please help me in solving my problem!!!
The code is as follows:
<html>
<title>
DBMS Project
</title>
<body bgcolor=#FFFFFF>
<font color=black>
<img src="logo.JPG" width="280" height="120"/>
</font>
<br>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://url.com:3306/DB", "guest", "1234567");
%>
<%
String action = request.getParameter("action");
if(action != null && action.equals("insert")) {
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(("INSERT INTO Table VALUES(?, ?, ?, ?, ?, ?, ?)"));
pstmt.clearParameters();
pstmt.setInt(1, Integer.parseInt(request.getParameter("Col1")));
pstmt.setString(2, request.getParameter("Col2"));
pstmt.setInt(3, Integer.parseInt(request.getParameter("Col3")));
pstmt.setString(4, request.getParameter("Col4"));
pstmt.setString(5, request.getParameter("Col5"));
pstmt.setString(6, request.getParameter("Col6"));
pstmt.setString(7, request.getParameter("Col7"));
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
}
%>
<%
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Table");
%>
<table border="1" cellspacing="6" >
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
<th>Col6</th>
<th>Col7</th>
</tr>
<tr>
<form action="Page.jsp" method="get">
<input type="hidden" value="insert" name="action">
<th><input value="" name="Col1" size="9"></th>
<th><input value="" name="Col2" size="10"></th>
<th><input value="" name="Col3" size="15"></th>
<th><input value="" name="Col4" size="15"></th>
<th><input value="" name="Col5" size="10"></th>
<th><input value="" name="Col6" size="10"></th>
<th><input value="" name="Col7" size="10"></th>
<th><input type="submit" value="Insert"></th>
</form>
</tr>
<%
while(rs.next()) {
%>
<tr>
<td> <%= rs.getInt("col1") %></td>
<td> <%= rs.getString("col2") %></td>
<td> <%= rs.getInt("col3") %></td>
<td> <%= rs.getString("col4") %></td>
<td> <%= rs.getString("col5") %></td>
<td> <%= rs.getString("col6") %></td>
<td> <%= rs.getString("col7") %></td>
</tr>
<%
}
%>
</table>
<%
rs.close();
stmt.close();
conn.close();
} catch (SQLException sqle) {
out.println(sqle.getMessage());
} catch (Exception e) {
out.println(e.getMessage());
}
%>
</body>
</html>Thank You!!!
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,625
Reputation:
Rep Power: 12
Solved Threads: 311
Few thinks to this
1) JSP should not be use any more to communicate with DB, you need to use servlets
2) Do not use GET method for action POST is better
3) You have no validation on your data, which is very bad (don't expect user to always provide correct data, even if this is school work. Teachers like to check on this sort of mistakes)
Check Sun website and these tutorials on how to do it
1) JSP should not be use any more to communicate with DB, you need to use servlets
2) Do not use GET method for action POST is better
3) You have no validation on your data, which is very bad (don't expect user to always provide correct data, even if this is school work. Teachers like to check on this sort of mistakes)
Check Sun website and these tutorials on how to do it
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
Publilius Syrus
(~100 BC)
LJC - London Java Community, JAVAWUG (Java Web User Group), Coding the Architecture
![]() |
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Understanding inserting data into an Access table using VB.NET (VB.NET)
- retrieving a single cell of data from a MySQL database (PHP)
- How to save data from a variable to a table. (PHP)
- server notify client to update MySQL table (C)
- Inserting datas from two tables to third table with a command. is it possible (MySQL)
- Automate Data from text file into MySql table? (MySQL)
- mysql < CSV < mydql (MySQL)
- how to update data in mysql table using data from an excel table (MySQL)
Other Threads in the JSP Forum
- Previous Thread: dynamic treeview
- Next Thread: Project



Linear Mode