Insert JSP form into MySQL

I tried this code but it got this error.

org.apache.jasper.JasperException: Exception in JSP: /tutorials/insert_test.jsp:143

140: Driver DriverInsert = (Driver)Class.forName(MM_db_DRIVER).newInstance();

141: Connection ConnInsert = DriverManager.getConnection(MM_db_STRING,MM_db_USERNAME,MM_db_PASSWORD);

142: PreparedStatement Insert = ConnInsert.prepareStatement("INSERT INTO novoim.chat (words) VALUES ('text') ");

143: Insert.executeUpdate();

144: %><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

145: <html xmlns="http://www.w3.org/1999/xhtml">

146: <head>

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<%@ include file="Connections/db.jsp" %>

<%

// *** Edit Operations: declare variables

// set the form action variable

String MM_editAction = request.getRequestURI();

if (request.getQueryString() != null && request.getQueryString().length() > 0) {

String queryString = request.getQueryString();

String tempStr = "";

for (int i=0; i < queryString.length(); i++) {

if (queryString.charAt(i) == '<') tempStr = tempStr + "<";

else if (queryString.charAt(i) == '>') tempStr = tempStr + ">";

else if (queryString.charAt(i) == '"') tempStr = tempStr + """;

else tempStr = tempStr + queryString.charAt(i);

}

MM_editAction += "?" + tempStr;

}

// connection information

String MM_editDriver = null, MM_editConnection = null, MM_editUserName = null, MM_editPassword = null;

// redirect information

String MM_editRedirectUrl = null;

// query string to execute

StringBuffer MM_editQuery = null;

// boolean to abort record edit

boolean MM_abortEdit = false;

// table information

String MM_editTable = null, MM_editColumn = null, MM_recordId = null;

// form field information

String[] MM_fields = null, MM_columns = null;

%>

<%

// *** Insert Record: set variables

if (request.getParameter("MM_insert") != null && request.getParameter("MM_insert").toString().equals("form1")) {

MM_editDriver= MM_db_DRIVER;

MM_editConnection = MM_db_STRING;

MM_editUserName= MM_db_USERNAME;

MM_editPassword= MM_db_PASSWORD;

MM_editTable = "novoim.chat";

MM_editRedirectUrl = "";

String MM_fieldsStr = "text|value";

String MM_columnsStr = "words|',none,''";

// create the MM_fields and MM_columns arrays

java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_fieldsStr,"|");

MM_fields = new String[tokens.countTokens()];

for (int i=0; tokens.hasMoreTokens(); i++) MM_fields = tokens.nextToken();

tokens = new java.util.StringTokenizer(MM_columnsStr,"|");

MM_columns = new String[tokens.countTokens()];

for (int i=0; tokens.hasMoreTokens(); i++) MM_columns = tokens.nextToken();

// set the form values

for (int i=0; i+1 < MM_fields.length; i+=2) {

MM_fields[i+1] = ((request.getParameter(MM_fields)!=null)?(String)request.getParameter(MM_fields):"");

}

// append the query string to the redirect URL

if (MM_editRedirectUrl.length() != 0 && request.getQueryString() != null) {

MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + request.getQueryString();

}

}

%>

<%

// *** Insert Record: construct a sql insert statement and execute it

if (request.getParameter("MM_insert") != null) {

// create the insert sql statement

StringBuffer MM_tableValues = new StringBuffer(), MM_dbValues = new StringBuffer();

String[] MM_dbValues_prep = new String[MM_fields.length/2];

for (int i=0; i+1 < MM_fields.length; i+=2) {

String formVal = MM_fields[i+1];

String elem;

java.util.StringTokenizer tokens = new java.util.StringTokenizer(MM_columns[i+1],",");

elem = (String)tokens.nextToken(); // consume the delim

String altVal= ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";

String emptyVal = ((elem = (String)tokens.nextToken()) != null && elem.compareTo("none")!=0)?elem:"";

if (formVal.length() == 0) {

if(emptyVal.equals("NULL")) {

formVal = null;

} else if(emptyVal.charAt(0) == '\'') {

formVal = emptyVal.substring(1, emptyVal.length()-1);

} else {

formVal = emptyVal;

}

} else if (altVal.length() != 0) {

if(altVal.charAt(0) == '\'') {

formVal = altVal.substring(1, altVal.length()-1);

} else {

formVal = altVal;

}

}

MM_dbValues_prep[i/2] = formVal;

MM_tableValues.append((i!=0)?",":"").append(MM_columns);

MM_dbValues.append((i!=0)?",":"").append('?');

}

MM_editQuery = new StringBuffer("insert into " + MM_editTable);

MM_editQuery.append(" (").append(MM_tableValues.toString()).append(") values (");

MM_editQuery.append(MM_dbValues.toString()).append(")");

if (!MM_abortEdit) {

// finish the sql and execute it

Driver MM_driver = (Driver)Class.forName(MM_editDriver).newInstance();

Connection MM_connection = DriverManager.getConnection(MM_editConnection,MM_editUserName,MM_editPassword);

PreparedStatement MM_editStatement = MM_connection.prepareStatement(MM_editQuery.toString());

for(int i=0; i<MM_dbValues_prep.length; i++) {

MM_editStatement.setObject(i+1, MM_dbValues_prep);

}

MM_editStatement.executeUpdate();

MM_connection.close();

// redirect with URL parameters

if (MM_editRedirectUrl.length() != 0) {

response.sendRedirect(response.encodeRedirectURL(MM_editRedirectUrl));

return;

}

}

}

%>

<%

Driver Drivertest2 = (Driver)Class.forName(MM_db_DRIVER).newInstance();

Connection Conntest2 = DriverManager.getConnection(MM_db_STRING,MM_db_USERNAME,MM_db_PASSWORD);

PreparedStatement Statementtest2 = Conntest2.prepareStatement("SELECT * FROM novoim.chat");

ResultSet test2 = Statementtest2.executeQuery();

boolean test2_isEmpty = !test2.next();

boolean test2_hasData = !test2_isEmpty;

Object test2_data;

int test2_numRows = 0;

%>

<%

Driver DriverInsert = (Driver)Class.forName(MM_db_DRIVER).newInstance();

Connection ConnInsert = DriverManager.getConnection(MM_db_STRING,MM_db_USERNAME,MM_db_PASSWORD);

PreparedStatement Insert = ConnInsert.prepareStatement("INSERT INTO novoim.chat (words) VALUES ('text') ");

Insert.executeUpdate();

%><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<title>Untitled Document</title>

</head>

<body>

<form name="form1" id="form1" method="POST" action="<%=MM_editAction%>">

<label>

<input name="text" type="text" id="text" size="110" maxlength="500" />

</label>

<label>

<input type="submit" name="send" id="send" value="send" />

</label>

<input type="hidden" name="MM_insert" value="form1" />

</form>

</body>

</html>

<%

test2.close();

Statementtest2.close();

Conntest2.close();

%>

<%

ConnInsert.close();

%>

[7839 byte] By [zerokidza] at [2007-11-27 11:01:03]
# 1

Let me make it simple

A code snap shot for you....

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection conn=DriverManager.getConnection("jdbc:odbc:HomeStay");

Statement stmt =conn.createStatement();

String query = "SELECT * from Family WHERE FAMILY_NAME ='"+FamilyName + "'";

ResultSet rst=stmt.executeQuery(query);

query = "INSERT INTO FAMILY VALUES('"+FamilyName + "', " +

" '" + MotherName +"', "+

"'" + MotherDOB +"', "+ .........);

int cnt = stmt.executeUpdate(query);

stmt.close();

conn.close();

Hope this helps in refactoring your code...

One more small advice,, handling DB connections on a JSP is not any more practised.

Cheers

Ranjith

ranjith98a at 2007-7-29 12:33:41 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...