How insert data to MySQL for using JSP code?

I have 1st time do JSP code, hope somebody can help me solve my problem..thk!

My code are below:

JSP file:input.jsp

<%@ page contentType="text/html" %>

<%@ page language="java" contentType="text/html" import="java.sql.*" %>

<%@ page import="java.io.*" %>

<%@ page import="java.util.*" %>

<html>

<head>

<title> User Info Entry Form</title>

</head>

<body>

<%

// 1. load the mysql jdbcc driver

Class.forName("com.mysql.jdbc.Driver");

// 2. open a connection to the "project" database

Connection Conn = DriverManager.getConnection

("jdbc:mysql://localhost/project?user=root&password=root");

// 3. Create a statement object for sending the SQL queries

Statement Stmt = Conn.createStatement();

// 4. place query results in a ResultSet object

String courseworkid= request.getParameter("coursework_id");

String unitcode = request.getParameter("unit_code");

String description1 = request.getParameter("description");

String actualmark= request.getParameter("actual_mark");

String weight1 = request.getParameter("weight");

PreparedStatement ps=Conn.prepareStatement("INSERT INTO coursework VALUES (?,?,?,?,?)");

ps.setString(1,courseworkid );

ps.setString(2,unitcode );

ps.setString(3,description1 );

ps.setInt(4,actualmark);

ps.setInt(5,weight1);

ps.executeUpdate();

ps.clearParameters();

// 5. Clean up all objects

Conn.close();

Stmt.close();

ps.close();

}catch(Exception e)

{

out.println(e);

}

%>

</body>

</html>

Message was edited by:

mike_yong

Message was edited by:

mike_yong

[1858 byte] By [mike_yonga] at [2007-11-26 15:37:39]
# 1

Another is my html file:2.html

<html>

<head>

<meta http-equiv="Content-Language" content="en-ie">

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<meta name="GENERATOR" content="Microsoft FrontPage 6.0">

<meta name="ProgId" content="FrontPage.Editor.Document">

<title>Input Data</title>

</head>

<body>

<h1> Student Coursework Mark Tracking System</h1>

<form method="POST" action="input.jsp" >

<p align="center">Coursework ID: <input type="text" name="coursework_id">

<p align="center"> Unit Code:

<input type="password" name="unit_code" size="22">

<p align="left">Coursework Description: <input type="text" name="description">

<p align="center"> Actual Mark:

<input type="text" name="actual_mark">

<p align="center">Weight: <input type="text" name="weight">?

<p align="left">

<input type="submit" value="ok" name="Ok">

<input type="submit" value="Cancel" name="cancel">

</form>

</body>

</html>

To u have any idea or suggestion(example) to guide me solve my insert database problem?

Message was edited by:

mike_yong

Message was edited by:

mike_yong

mike_yonga at 2007-7-8 21:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi Mike Yong, I think you should reorder your statements in the code.ps.close()Conn.close(); instead of Conn.close();stmt.close();ps.close(); Please post your exception clearly.Thanks,LALITH
tochprasada at 2007-7-8 21:55:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...