Connection to mysql using jsp/jdbc
Posted by: Anitha
Date: May 16, 2006 05:58AM

Hai,

I am writting jsp and Html using JDBC in single file.
while I am excuting that file first null value is inserting into database.
then required from is displaying and entered value is inserting into database(City table)

Please Solve my Problem

Code: I am creating a City Form and entering values into City database.(City.jsp)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.sql.*"%>
<%@page import="java.sql.Driver"%>
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>City.jsp</TITLE>
</HEAD>
<BODY>
<h1>City</h1>
<form method=post action=/RedCabs/City.jsp>
Name.:<input type=text name=CityName size=15 style=bgcolor:lightblue><br><br>
<input type=submit value=Submit></form>
<jsp:useBean id="db" scope="request" class="SQLBean.ConnectionBean" />
<jsp:setProperty name="db" property="*" />
<%!
ResultSet rs = null ;
int i;
%>
<%
String CityName=request.getParameter("CityName");
System.out.println(CityName);
out.println(CityName);
try{
System.out.println("before connection");
db.connect();
System.out.println("after connection");
i = db.updateSQL("INSERT into City(CityName) VALUES('"+CityName+"')");

}catch(SQLException e) {
throw new ServletException("Your query is not working", e);
}
%>
</BODY>
</HTML>


ConnectionBean.java

package SQLBean;
import java.util.*;
import java.sql.*;



public class ConnectionBean {

private String driver = new String("com.mysql.jdbc.Driver");
private String url= new String("jdbc:mysql://192.168.1.40/test");
private String user = new String("root");
private String password= new String("password");

private Connection dbCon;

public ConnectionBean(){
super();
}

public boolean connect() throws ClassNotFoundException,SQLException{
Class.forName(driver);
dbCon = DriverManager.getConnection(url,user,password);
return true;
}

public void close() throws SQLException{
dbCon.close();
}
public int updateSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
int r = s.executeUpdate(sql);
return (r == 0) ? 0 : r;
}
public ResultSet execSQL(String sql) throws SQLException{

Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return (r == null) ? null : r;
}



}

Options: ReplyQuote


Subject
Written By
Posted
November 27, 2005 05:27AM
September 24, 2006 10:26AM
December 27, 2007 11:53PM
December 31, 2007 09:54PM
February 19, 2006 11:57PM
Connection to mysql using jsp/jdbc
May 16, 2006 05:58AM
b g
June 09, 2009 04:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.