Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

insert into table query in jsp  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found some problem for this jsp code.Actually i'm new in jsp.need some help.I'm trying to insert some data in a sql table in jsp by user's my code but i couldn't completed it.please help me.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>%>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
String deptno=null;
String dname= request.getParameter("dname");
String loc= request.getParameter("loc");

String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:oracle";

Connection con=null;
Statement st=null;


String query="insert into member values(Concat('C',Lpad(To_Char(m_seq.Nextval),3,'0')),"+"'"+dname+"','"+loc+")";
int val=st.executeUpdate(query);
}catch(Exception e)

Author and ninkuma
Posts: 66425
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are new to JSP, then you need to know that you are doing it wrong. Putting Java code in a JSP is a bad practice that has been obsolete and discredited for over 12 years. you should not be putting Java code into JSPs.

Any Java code should be in Java classes. Within the JSP, the EL (Expression Language) and JSTL (JSP Standard Tag Library) should be used.

I always recommend that newcomers to JSP read there articles:
  • The Secret Life of JSPs
  • The Front Man

    Posts: 23282
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And if you're learning how to use JDBC, then you should start by writing some ordinary Java code to do that. Using a JSP adds at least two layers of complexity which you don't need. If you're going to learn juggling, then don't start out by trying to juggle flaming chainsaws.

    If you have trouble with that JDBC code, then certainly you can post that question here too. We have a JDBC forum for that purpose. Welcome to the Ranch!

    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As mentioned already, in practice it is not a great idea to have much Java code in your JSP, but I can understand you doing it for a test or learning process.

    As I constantly tell people, properly line up your code so people can see it.

    When developing a JSP page, as with most code, do it piece by piece and test each piece.

    When referring to table names and columns, I always advocate for having constants containing those names so that you do not find that in one place you have mis-typed it.

    You also need to close your connection when you are done with it.

    When you catch an exception, try to print out more information to help with debug, such as the stack trace, the name of the class of the exception, the local message, and the message.

    You say you need help, but do not state what you need help with. Where is your problem?

    You will also want to note that you have an extra %> in your import section.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!