• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

add data to database using JSP script

 
Sukri Mohd
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had try this code but have error...
help me to solve this problem..

code:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>Kemasukan Data Pelajar Praktikal</title>
</head>
<body bgcolor="#CCCCCC"><div align="center">

<%


String namapelajar = request.getParameter("namapelajar");
String noic = request.getParameter("noic");
String studdd = request.getParameter("studdd");
String studmm = request.getParameter("studmm");
String studyy = request.getParameter("studyy");
String add = request.getParameter("add");
String nofon = request.getParameter("nofon");
String nomatrik = request.getParameter("nomatrik");
String orgname = request.getParameter("orgname");
String orgpenyelia = request.getParameter("orgpenyelia");
String orgdd = request.getParameter("orgdd");
String orgmm = request.getParameter("orgmm");
String orgyy = request.getParameter("orgyy");
String namaprojek = request.getParameter("namaprojek");
String tempohprojek = request.getParameter("tempohprojek");
String keteranganprojek = request.getParameter("keteranganprojek");
String lawatandd = request.getParameter("lawatandd");
String lawatanmm = request.getParameter("lawatanmm");
String lawatanyy = request.getParameter("lawatanyy");
String lawatancatatan= request.getParameter("lawatancatatan");
String penilaianpenyelia= request.getParameter("penilaianpenyelia");
String penilaianjawatan= request.getParameter("penilaianjawatan");
String penilaianpotensi= request.getParameter("penilaianpotensi");
String penilaiankelemahan= request.getParameter("penilaiankelemahan");
String penilaiancatatan= request.getParameter("penilaiancatatan");



boolean proceed = true;

// connecting to database

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
String sql;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con = DriverManager.getConnection("jdbc racle:thin:@l0.10.1.26:1521 evdb", "ictdev", "ictdev");
sql = "INSERT INTO PELAJAR_PRAKTIKAL(NAMA_PELAJAR,NO_IC,STUD_DD,
STUDDMM,STUDYY,ADDRESS,NO_FON,NO_MATRIK,ORG_NAME,ORG_PENYELIA,ORG_DD,
ORG_MM,ORG_YY,NAMA_PROJEK,TEMPOH_PROJEK,KETERANGAN_PROJEK,
LAWATAN_DD,LAWATAN_MM,LAWATAN_YY,LAWATAN_CATATAN,
PENILAIAN_PENYELIA,PENILAIAN_JAWATAN,PENILAIAN_POTENSI,
PENILAIAN_KELEMAHAN,PENILAIAN_CATATAN) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
if(proceed)
{
ps.setString(1, namapelajar);
ps.setString(2, noic);
ps.setString(3, studdd);
ps.setString(4, studmm);
ps.setString(5, studyy);
ps.setString(6, add);
ps.setString(7, nofon);
ps.setString(8, nomatrik);
ps.setString(9, orgname);
ps.setString(10, orgpenyelia);
ps.setString(11, orgdd);
ps.setString(12, orgmm);
ps.setString(13, orgyy);
ps.setString(14, namaprojek);
ps.setString(15, tempohprojek);
ps.setString(16, keteranganprojek);
ps.setString(17, lawatandd);
ps.setString(18, lawatanmm);
ps.setString(19, lawatanyy);
ps.setString(20, lawatancatatan);
ps.setString(21, penilaianpenyelia);
ps.setString(22, penilaianjawatan);
ps.setString(23, penilaianpotensi);
ps.setString(24, penilaiankelemahan);
ps.setString(25, penilaiancatatan);

ps.executeUpdate();
}
}
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.println("SQLException: " + e.getMessage() + "<BR>");
while((e = e.getNextException()) != null)
out.println(e.getMessage() + "<BR>");
}

}
%>
<h3>REKOD BERJAYA DISIMPAN</h3>
</body>
</html>
[ April 05, 2006: Message edited by: Bear Bibeault ]
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the problem?
What is the problem?
It would be great if you can add stacktrace/comilation error to answer above questions....


Shrinivas
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Structure of your code is like this:



try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
/*

Your JDBC code goes here

*/
}
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) { }

}



Though you have put your main JDBC code in try block but at end of try block you have just used finally{} to release JDBC resources. Before finally{} you also need to catch SQLException thrown by your JDBC code


Jass
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic