aspose file tools*
The moose likes JDBC and the fly likes problem with insertion.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem with insertion.." Watch "problem with insertion.." New topic
Author

problem with insertion..

jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
iam not being able to insert the values in database..
here is the code for insertion:

<%@ page language="java" import="java.sql.*,java.io.*" %>
<%
Connection con=null;
java.sql.Statement stmt=null;
String paper_title="";
String co_authors="";
int year=0;
String citation="";
String username="";

String xy=(String)session.getAttribute("y");
if(xy==null)
xy="jasu";

username= request.getParameter("username");
paper_title=request.getParameter("paper_title");

co_authors=request.getParameter("co_authors");
year=Integer.parseInt(request.getParameter("year"));
citation=request.getParameter("citation");

out.println(username);

System.out.println("the value of paper is:"+paper_title);
System.out.println("the value of co is:"+co_authors);
System.out.println("the value of citation is:"+citation);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:mech_iit");
stmt=con.createStatement();
int i=stmt.executeUpdate("insert into Papers(paper_title,co_authors,year,citation) values('"+paper_title+"','"+co_authors+"',"+year+",'"+citation+"') where user_login='"+username+"')");
System.out.println("the value of:" +i);
stmt.close();

con.close();
}
catch(Exception e)
{
System.out.println(e);

}

%>

inthis when the user logs in he can insert his papers.. bt in the tomcat window.. iam able to see the values which the user inserts bt not the username..
and moreover it gives a sqlexception that
incorrect syntax near the keyword 'where'
.. means i guess session is nt being maintained properly..
pls help..
thanks
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

Hi Jyotsana,
Your INSERT statement is wrong. This is a JDBC problem (or more specifically, an SQL problem), rather than a JSP problem. In SQL, there are no WHERE clauses in INSERT statements. So just remove the WHERE clause from your INSERT statement, as in:

Good Luck,
Avi.
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
Hi Avi..
thanks for replying..
bt i need to check the insertion against the username...only if the user has logged in, should he be able to insert his paper..
i need to maintain the session..
and the tables i created for this purpose, have user_login also as the column like paper table has user_login, paper_title etc. as the columns..
and i have given a not null constraint in the user_login..
so if i remove the where clause.. it gives an exception.. can't insert null value to user_login..insert fails
any guidance.. in this..iam fairly new to jsp ..
thanks
[ November 25, 2003: Message edited by: jyotsana dang ]
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
Hi..
If the user_login already has some value, you only need UPDATE Statement the table, else you insert everything into the Paper table, as pointed by Avi, there can never be a where in INSERT Statements.
For checking wether the user has already logged in, you just check it out with the session.
hth
MB


Malhar Barai
SOA & Java Book
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Moving to the JDBC forum.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jyotsana dang
Ranch Hand

Joined: Sep 26, 2003
Posts: 135
thanks everyone..
bt the problem still remains.
.. i made changes to the insert stmt..
bt ..
in this page i have 3 textfields .. paper_title,authors, year, citation..
whatever value i give in the paper_title .. it gives an error in the same..
if i give "one" as the value..
it shows the error as
:
the value of paper_title ne
the value of co is:two
the value of citation is:three
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: In
correct syntax near 'one'.

here's the code again:

<%@ page language="java" import="java.sql.*,java.io.*" %>
<%
Connection con=null;
java.sql.Statement stmt=null;
String paper_title="";
String co_authors="";
int year=0;
String citation="";
String username="";the value of paper is ne

String xy=(String)session.getAttribute("y");
if(xy==null)
xy="jasu";
paper_title=request.getParameter("paper_title");

co_authors=request.getParameter("co_authors");
year=Integer.parseInt(request.getParameter("year"));
citation=request.getParameter("citation");

out.println(username);

System.out.println("the value of paper is:"+paper_title);
System.out.println("the value of co is:"+co_authors);
System.out.println("the value of citation is:"+citation);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:mech_iit");
stmt=con.createStatement();
out.println("insert into Papers(user_login,paper_title,co_authors,year,citation) values('"+xy+"'"+paper_title+"','"+co_authors+"',"+year+",'"+citation+"')");

int i=stmt.executeUpdate("insert into Papers(user_login,paper_title,co_authors,year,citation) values('"+xy+"'"+paper_title+"','"+co_authors+"',"+year+",'"+citation+"')");
System.out.println("the value of:" +i);
stmt.close();

con.close();
}
catch(Exception e)
{
System.out.println(e);

}

%>
 
 
subject: problem with insertion..