Results 1 to 6 of 6
- 04-02-2010, 05:26 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Problem regarding Inserting data into Mysql via JSP
Hi i'm new to mysql and jsp conectivity..but i have studied a few tutorials to start with..
While trying to insert data into mysql after creating database and a table in mysql, i am unable to insert data into mysql db.
syntax for table creation:
create table user_master (
user_id int not null auto_increment,
name varchar(20),
comment text(200),
primary key(user_id)
);
here is my program:-
first_page.jsp
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>Create table in mysql database using jsp</title>
</head>
<body bgcolor="#ffffcc">
<form action="ConnectJspToMysql.jsp" method="get">
<TABLE style="background-color: #ECE5B6;" WIDTH="40%">
<tr>
<td >Student Id</td>
<td ><input type="text" name="id" SIZE="35"></td>
</tr>
<tr>
<td >Student Name</td>
<td ><input type="text" name="name" SIZE="35"></td>
</tr>
<tr width="100%">
<td >Comments<br><font color="blue">
(max 200 chars)</font></td>
<td ><TEXTAREA NAME="comment" ROWS="5" COLS="26">
</TEXTAREA></td>
</tr>
<tr><td></td>
<td><input type="reset" name="reset"
value="reset">
<input type="submit" value="submit"></td></tr>
</TABLE>
</form>
</body>
</html>
and
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>data successfully saved</title>
</head>
<body>
<% // get parameters from the request
String id = request.getParameter("id");
String name = request.getParameter("name");
String comment = request.getParameter("comment");
Statement statement = null;
// declare a connection by using Connection interface
Connection connection = null;
try {
/* Create string of connection url within specified format
with machine name,
port number and database name. Here machine name id
localhost and database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost:3306/usermaster";
// Load JBBC driver "com.mysql.jdbc.Driver" by newInstance() method.
Class.forName("com.mysql.jdbc.Driver").newInstance ();
/* Create a connection by using getConnection()
method that takes parameters of string type connection url, user
name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
String QueryString = "INSERT INTO user_master
(user_id,name,comment) VALUES ('" +
id + "','" + name + "','" + comment + "')";
int UQ = statement.executeUpdate(QueryString);
if (UQ > 0) {
%><font color="green" size="5" >Congratulations !</font>
<h2><p>Your comment is submitted successfully.<br></p></h2>
<TABLE style="background-color: #ECE5B6;" WIDTH="30%">
<tr>
<tr>
<td>User Id</td>
<td><%= id%></td>
</tr>
<tr>
<td>Name</td>
<td><%=name%></td>
</tr>
<tr><td></td><td align="right">
<A HREF="first_page.jsp">
<font size="4" color="blue">go to home page</font></A>
</td>
</tr>
</TABLE>
<% }
} catch (Exception ex) {
%>
<FONT size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
} finally {
statement.close();
connection.close();
}
%>
</FONT>
</body>
</html>
or refer to Submit comments in database when user clicks on submit button
After executing/running it via apache-tomcat im getting the output as UNABLE to connect to database.....
Please can anyone help me out....
- 04-02-2010, 05:49 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you sure that your DB connection is correct, password and all stuff?
- 04-02-2010, 07:10 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Yes it is correct...
as i tried it with other programs of retrieving data and displaying it ....\
But im not able to insert data into MYSQL db..
:(
- 04-03-2010, 04:09 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you post the compete error message here to see?
- 04-05-2010, 05:44 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
Hey thanks as i got my db workin properly...:D
- 04-05-2010, 01:24 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine, then please mark the thread solved from the tools menu.
Similar Threads
-
Now stuck with inserting data into a list.
By mainy in forum New To JavaReplies: 2Last Post: 07-31-2009, 03:49 PM -
Inserting data to database from the form
By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-17-2008, 01:06 AM -
Inserting data containing quotes into DB
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:28 AM -
how to call data from MySQL
By rhlpnt in forum Java ServletReplies: 0Last Post: 02-02-2008, 11:03 AM -
Problem inserting values to MySQL tables from the Data Source Explorer
By tip in forum EclipseReplies: 0Last Post: 12-24-2007, 09:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks