Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: Connection to mysql using jsp


Advanced Search

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
Connection to mysql using jsp arun chanumalla 04/04/2005 10:50AM
Re: Connection to mysql using jsp Sudhir Movva 04/05/2005 01:36PM
Re: Connection to mysql using jsp Megha Bhagat 04/18/2005 03:30AM
Re: Connection to mysql using jsp yenne jagadeesh 11/15/2006 04:20AM
Re: Connection to mysql using jsp Bill Karwin 11/15/2006 10:57AM
connection to mysql using jsp junia phiong 11/27/2005 05:27AM
Connection to mysql using jsp bindu priyadarshini 09/24/2006 09:39AM
Re: Connection to mysql using jsp Bill Karwin 09/24/2006 10:26AM
Connection to mysql using jsp uma maheswari 12/27/2007 11:53PM
Re: Connection to mysql using jsp kuna Ram 12/31/2007 09:54PM
Re: Connection to mysql using jsp sukasom chaiyakul 04/06/2005 05:16AM
Re: Connection to mysql using jsp Ashish Patil 01/21/2006 07:59AM
Re: Connection to mysql using jsp Ashish Patil 01/21/2006 07:42AM
Re: Connection to mysql using jsp Vetri Selvan 02/15/2006 12:12AM
Re: Connection to mysql using jsp Arun Kumar 02/19/2006 11:57PM
Connection to mysql using jsp/jdbc Arun Kumar 02/20/2006 01:46AM
Re: Connection to mysql using jsp/jdbc raj kris 12/14/2007 01:11AM
Connection to mysql using jsp/jdbc Anitha 05/16/2006 05:58AM
Re: Connection to mysql using jsp Antonel Pazargic 02/20/2006 12:20AM
Re: Connection to mysql using jsp mitesh bhimjiyaani 05/17/2006 07:26AM
Re: Connection to mysql using jsp yenne jagadeesh 11/15/2006 04:15AM
Re: Connection to mysql using jsp labhshree kodhe 06/05/2006 12:41AM
Re: Connection to mysql using jsp sourav mukherjee 08/03/2007 06:24AM
Re: Connection to mysql using jsp sekhar babu patnam 08/08/2007 12:44AM
create System DSN in jsp? b g 06/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.