Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i insert data in to a database through java server pages.

Status
Not open for further replies.

johnpele

Technical User
Feb 6, 2003
7
IE
Here is the following code i am using

package com.stardeveloper.servlets.db;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.print(&quot;<html><body>&quot;);

out.print(&quot;<form action=\&quot;&quot;);
out.print( req.getRequestURI() );
out.print(&quot;\&quot; method=\&quot;post\&quot;>&quot;);
out.print(&quot;First Name :<br>&quot;);
out.print(&quot;<input type=\&quot;text\&quot; name=\&quot;first\&quot;><br>&quot;);
out.print(&quot;<br><br><input type=\&quot;submit\&quot; value=\&quot; \&quot;>&quot;);
out.print(&quot; Insert Record&quot;);
out.print(&quot; <input type=\&quot;submit\&quot; value=\&quot; \&quot;>&quot;);
out.print(&quot; Display Records</form>&quot;);

out.print(&quot;</body></html>&quot;);

out.close();
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType(&quot;text/html&quot;);
PrintWriter out = res.getWriter();

out.print(&quot;<html><body>&quot;);

out.print(&quot;<code><pre>&quot;);
out.println(&quot;ID\tFirst Name\n&quot;);

// receiving parameters

String first = req.getParameter(&quot;first&quot;).trim();
boolean proceed = false;

if(first != null && last != null)
if(first.length() > 0 && last.length() > 0)
proceed = true;

// connecting to database

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;

try {
Load Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database
String dbaddress = (&quot;jdbc:eek:racle:thin:mad:macha.wit.ie:1521:MACH&quot;);
Connection Conn = DriverManager.getConnection (&quot;jdbc:eek:racle:thin:mad:macha.wit.ie:1521:MACH&quot;, &quot;W99402734&quot;,&quot;UBWELE&quot;);


Statement Stmt = Conn.createStatement();
String sql;

sql = &quot;INSERT INTO interest(first_name) VALUES (john)&quot;;
ps = con.prepareStatement(sql);
stmt = con.createStatement();

// inserting records

if(proceed) {
ps.setString(1, first);
ps.executeUpdate();
}

// displaying records

rs = stmt.executeQuery(&quot;SELECT * FROM interest&quot;);
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print(&quot;\t&quot;);
out.print(&quot;\t&quot;);
out.print(&quot;\n&quot;);
}


} catch (SQLException e) {
throw new ServletException(e);
} catch (ClassNotFoundException e) {
throw new ServletException(e);
} finally {
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(ps != null)
ps.close();
if(con != null)
con.close();
} catch (SQLException e) {}
}

out.print(&quot;</body></html>&quot;);
out.close();
}
}
 
sql = &quot;INSERT INTO interest(first_name) VALUES (john)&quot;;

sql = &quot;INSERT INTO interest(first_name) VALUES (?)&quot;;

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top