• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with insertion..

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the JDBC forum.
bear
 
jyotsana dang
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}

%>
 
reply
    Bookmark Topic Watch Topic
  • New Topic