Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting data to a table

 
Aarthi moorthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
In Jsp,
I want to insert a documentid,userid from a form when it is posted to the same page to a table..
But when it gets inserted a null value is also inserted in another row of the table with same documentid but userid to null..
why this happens....?
 
Joseph Vinodh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai!
I too am surprised why it populates another row with duplicate document id and null user id.
Just make sure you have followed similar steps as shown here.
Which JDBC driver and RDBMS u r using?
// DBConnection has a valid java.sql.Connection value.
try{
String query = "insert into Table values(?,?)";
PreparedStatement ps = DBConnection.prepareStatement(query);
ps.setInt(1,docId); //docId should be the request Parameter
ps.setString(2,userId);
ps.executeUpdate();
ps.close();
}catch(SQLException sql){//Handle the exception}
Try this and let me know what happens.
 
Aarthi moorthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joseph Vinodh,
Thanks for ur reply..
I am using JDBC connection only....
I used this coding to fix that null row to be added in the table,
try
{
String jj="";

String gg1=request.getParameter("gg");
out.print(gg1);
if (gg1.equals(jj))
{
out.print("nullllllllll");
}
else
{
st.executeUpdate("insert into shareddocuments(documentid,userid) values('"+s+"','"+gg1+"')");
out.println("INSERTED....");
}
}
catch (Exception e)
{
out.println("errror"+e);
}
but i get
this error

java.lang.NullPointerException
But my progrm is working....
help me to fix this error..
regards,
Aarthi.
 
Joseph Vinodh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Aarthi,
I think there is no problem as for as Java coding is concerned.
R u sure that u have given value for "s"(user Id)and u should also make sure whether u assigned "gg" as your textbox name for userId and "s" as your textbox name for document id.
Still if u find it difficult, send me or post your entire jsp file .
Regards,
Vinodh.
 
Aarthi moorthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@page import="java.util.*,java.text.*" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>New Page 1</title>
</head>
<%!
Statement st;
ResultSet rs,rs2,rs1,rs3,rs4,rs5,rs6;
%>
<body topmargin="0" leftmargin="0">
<%
String id1=request.getParameter("id");
String s= request.getParameter("xx");
out.print(id1);
out.print(s);
Connection con=null;
String searchvalue="";
searchvalue=request.getParameter("search");

String s1=request.getParameter("b1");
String v1="";
String n,p,gg;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:extranet");
st=con.createStatement();
st.executeUpdate("delete * from shareddocuments where ID1="+id1 );
rs1=st.executeQuery("select * from documents where documentid ="+s);
out.print(session.getAttribute("SessionUsername"));
out.print(session.getAttribute("SessionUserid"));
%>
<center>
<table border="1" valign=top cellspacing=0 width="565" bordercolorlight="#000000" bordercolordark="#000000" height="161" cellpadding="0">
<tr>
<%out.print(s);%>
<td width="227" height="157" valign=top> <table border="0" width="241" height="164" bordercolorlight="#000080" bordercolordark="#000080" cellpadding="0" cellspacing="0">
<tr>
<td width="257" height="164" valign=top>
<table border="0" width="135" height="106" cellpadding=2 valign=top>
<tr>
<td valign=top height="96" >
<table border="0" cellspacing="0" cellpadding="0" bordercolorlight="#000000" height="116">
<%
while (rs1.next()) {
%>

<tr>
<td bgcolor="#808080" align="center" valign="top">
<p align="center"><font size="2" color="#FFFFFF">Document
Name</font>
</td>
<td align="left" valign="top">
<p align="center"><font size="2"><%=rs1.getString("documentname")%></font></p>
</td>
</tr>
<tr>
<td bgcolor="#808080" align="center">
<p align="center"><font size="2" color="#FFFFFF">Document
Details</font>

</td>
<td align="left" >
<p align="center"><font size="2"><%=rs1.getString("details")%></font>

</td>
</tr>
<%
}
%>
</table>
</td>
</tr>
</table>

</td>
</tr>
</table>
</td>
<td width="505" height="157" valign=top>
<table border="0" width="206" height="162" bordercolorlight="#000080" bordercolordark="#000080" cellpadding="0" cellspacing="0">
<tr>
<td width="196" height="162" valign=top>
<table border="0" width="201" height="125" cellpadding=0>
<tr>
<td width="201" height="125" valign=top>
<table border="0" height="121" cellspacing="3" cellpadding="0" width="296">
<tr>
<td width="286" colspan="2" align="center" bgcolor="#808080" height="14"><font color="#FFFFFF" size="2">Shared
List</font></td>
</tr>
<tr>
<td ><font size="2">Name Of
Person </font></td>
<td align="center"><font size="2">Delete</font></td>
</tr>
<%

rs2=st.executeQuery("select * from shareddocuments where shareddocuments.documentid= "+s);
while (rs2.next())
{
String c1=rs2.getString(1);
String c2=rs2.getString(2);
String c3=rs2.getString(3);

%>
<tr>
<td ><font size="2"><%=c3%></font></td>
<td align="center">
<form action=popup2.jsp>
<input type=hidden value="<%=c1%>" name=id>
<input type=hidden value="<%=s%>" name=xx>
<input type=image name=submit src="images/my_doc1.gif">
</font></td>
</tr>
<%
}
%>

</table>
</td>
</tr>
</table>
</table>
</table>
</center>
<div align="center">
<center>

<form method="POST" action="popup2.jsp">
<input type=text name=xx value=<%=s%>>
<table border="1" valign=top cellspacing=0 width="563" bordercolorlight="#000000" bordercolordark="#000000">
<tr>
<td width="557" valign=top align="center">
<table border="0" height="96" cellspacing="3" cellpadding="0" width="220">
<tr>
<td width="429" colspan="2" align="center" bgcolor="#808080" height="14"><font color="#FFFFFF" size="2">Search</font></td>
</tr>
<tr>
<td width="423" height="25" colspan="2" align="center" bgcolor="#999999"><font size="2">Organization</font></td>
</tr>
<tr>

<td width="384" height="1" align="center">



<%
String srt1="select * from security";
rs5=st.executeQuery(srt1);
%>
<select name=search class=body tabindex=1>
<% while(rs5.next())
{
String nn=rs5.getString(1);
String nn1=rs5.getString(2);
%>
<option value=<%=nn%>><%=nn1%></option>
<%
}
%>
</select>
</td>
<td align="center"><input type="submit" value="Search" name="b1" tabindex="2"></td>
<td width="36%" align="center"><input type="button" value="Back" name="b1" tabindex="3" ></td>
</tr>

<p>
<%
if (s1==null)
{
}
else if (s1.equals("Search"))
{
rs=st.executeQuery("select employee.userid,username from employee,login where login.securitycode like '"+searchvalue+"'+'%' and employee.userid=login.userid"); %>
<center>
<table border=1>
<tr><th>User id<th>Username</tr>
<%
while(rs.next())
{
n=rs.getString("userid");
//out.println(n);
p=rs.getString("username");
%>
<tr><td><%=n%><td><a href="popup2.jsp?gg=<%=n%>&xx=<%=s%>"><%=p%><a></tr>
<%
}
out.print(s);
%>
</table>
</center>
</td>
</tr>

</table>
</table>
</form>
</center>
<%
}
}
catch (Exception e)
{
out.println(e);
}

try
{
String jj="";

String gg1=request.getParameter("gg");
out.print(gg1);
if (gg1.equals(jj))
{
out.print("nullllllllll");
}
else
{
st.executeUpdate("insert into shareddocuments(documentid,userid) values('"+s+"','"+gg1+"')");
out.println("INSERTED....");
}
}
catch (Exception e)
{
out.println("errror"+e);
}
%>
</p>
</body>
I get the error as i said in the second my last reply.
 
Joseph Vinodh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly try this, just include one more condition in your IF as below
if (gg1.equals(jj)||gg1==null)
{
out.print("nullllllllll");
}
else
{
st.executeUpdate("insert into shareddocuments(documentid,userid) values('"+s+"','"+gg1+"')");
out.println("INSERTED....");
}
If success, let me know.
Regards,
Vinodh.
 
Aarthi moorthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vinod,
thanks ya!
Even i get the same error.....

errrorjava.lang.NullPointerException
regards,
aarthi.
 
Joseph Vinodh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i don't have your DB-related thing in my computer, i could not run and find out where the problem lies.
But still you can find out the problem by using out.println()fn after each and every executable statement.After you find out where NullPointer exception gets thrown, you could easily able to solve the problem.
Good Luck!!
Regards,
Vinodh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic