| | |
How to insert data into database?
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2005
Posts: 2
Reputation:
Solved Threads: 0
JSP is a new world for me, since 3 days ago. I am having a problem (or I have no idea) how does the inserting datas into database works. I have looked at some examples, but still, im having a hard time. I know PHP and ASP, but JSP is so new for me. Here's part of my codes:
------------------------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%
String firstname = request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2 = request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
ResultSet rs = null;
Statement statement = connection.createStatement();
String sql = ("INSERT INTO memory VALUES ('" + firstname + "','" + lastname + "','" + address1 + "','"+ address2 +"','"+ address3 +"','"+ city +"','"+ postalcode +"','"+ province +"','"+ memo +"') ");
statement.executeUpdate(sql);
rs.close();
connection.close();
%>---------------------------------------------------
Can someone help? Thanks....
------------------------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%
String firstname = request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2 = request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
ResultSet rs = null;
Statement statement = connection.createStatement();
String sql = ("INSERT INTO memory VALUES ('" + firstname + "','" + lastname + "','" + address1 + "','"+ address2 +"','"+ address3 +"','"+ city +"','"+ postalcode +"','"+ province +"','"+ memo +"') ");
statement.executeUpdate(sql);
rs.close();
connection.close();
%>---------------------------------------------------
Can someone help? Thanks....
•
•
Join Date: Aug 2004
Posts: 8
Reputation:
Solved Threads: 0
Hi
I find ur code correct.
whats the problem u r dealing with
Here r some defined steps to be followed for DB operation
1. Initialize the class with the specific driver
2. Create a connection object
3. Create statement
4. Execute query
5. Close the connection
eg. consider the DB in MsSql 2000 named 'Student' for table stud_info
<%
// Step 1
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Step 2
Connection conn = DriverManager.getConnection("jdbc:odbc:Student");
// here the DSN name for our database is 'Student'
//step 3
Statement stmt = conn.createStatement();
// No result set is required if you r Deleting, Updating or inserting Data to database
// To insert the Data just write the insert query and execute
%
hope this helps you
I find ur code correct.
whats the problem u r dealing with
Here r some defined steps to be followed for DB operation
1. Initialize the class with the specific driver
2. Create a connection object
3. Create statement
4. Execute query
5. Close the connection
eg. consider the DB in MsSql 2000 named 'Student' for table stud_info
<%
// Step 1
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Step 2
Connection conn = DriverManager.getConnection("jdbc:odbc:Student");
// here the DSN name for our database is 'Student'
//step 3
Statement stmt = conn.createStatement();
// No result set is required if you r Deleting, Updating or inserting Data to database
// To insert the Data just write the insert query and execute
%
hope this helps you
•
•
Join Date: May 2005
Posts: 2
Reputation:
Solved Threads: 0
I did this:
------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%!
String firstname= request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2= request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
Statement statement = connection.createStatement();
String sql = ("INSERT INTO memory VALUES ('" + firstname + "','" + lastname + "','" + address1 + "','"+ address2 +"','"+ address3 +"','"+ city +"','"+ postalcode +"','"+ province +"','"+ memo +"') ");
rs.close();
connection.close();
%>----------------------------------------------------------------------
But still, not working.
------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%!
String firstname= request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2= request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
Statement statement = connection.createStatement();
String sql = ("INSERT INTO memory VALUES ('" + firstname + "','" + lastname + "','" + address1 + "','"+ address2 +"','"+ address3 +"','"+ city +"','"+ postalcode +"','"+ province +"','"+ memo +"') ");
rs.close();
connection.close();
%>----------------------------------------------------------------------
But still, not working.
You should also move things like database operations out of the JSP and into Javabeans and/or servlets.
Ideally a JSP should only concern itself with displaying data that was retrieved and/or calculated in a servlet.
Ideally a JSP should only concern itself with displaying data that was retrieved and/or calculated in a servlet.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Solved Threads: 0
I hope this would work
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%!
String firstname= request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2= request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
PreparedStatement ps=conn.prepareStatement("INSERT INTO memory VALUES (?,?,?,?,?,?,?,?)");
ps.setString(1,firstname );
ps.setString(2,lastname );
ps.setString(3,address1 );
ps.setString(4,address2 );
ps.setString(5,address3 );
ps.setString(6,postalcode );
ps.setString(7,province );
ps.setString(8,memo );
ps.execute();
conn.close();
}catch(Exception e)
{
out.println(e);
}
%>-------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%!
String firstname= request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2= request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
%>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
PreparedStatement ps=conn.prepareStatement("INSERT INTO memory VALUES (?,?,?,?,?,?,?,?)");
ps.setString(1,firstname );
ps.setString(2,lastname );
ps.setString(3,address1 );
ps.setString(4,address2 );
ps.setString(5,address3 );
ps.setString(6,postalcode );
ps.setString(7,province );
ps.setString(8,memo );
ps.execute();
conn.close();
}catch(Exception e)
{
out.println(e);
}
%>-------------------------------------------
•
•
Join Date: Jun 2008
Posts: 1
Reputation:
Solved Threads: 0
Very similar problem with mine.
Everything workk, but no with memo or blob field transfer. If memo length is less then 255 everything is working.
I need similar transfer from Visual Foxpro via ODBC connector and my code is:
-MySQLstr="DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;"+"PORT=3306 ;DATABASE=xxx;USER=yyy;"+"PASSWORD="+"zzzz"+";OPTION=3;"
-MySQLhandle=sqlstringconnect(MySQLstr)
-SQLEXEC(MySQLhandle, 'INSERT INTO xx (name,date,memo) VALUES ("Johns","03:04:2005",MEMO)')
IF there is MEMO it works only if it it shortes than 255, or if MEMO i s there not !
Everything workk, but no with memo or blob field transfer. If memo length is less then 255 everything is working.
I need similar transfer from Visual Foxpro via ODBC connector and my code is:
-MySQLstr="DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;"+"PORT=3306 ;DATABASE=xxx;USER=yyy;"+"PASSWORD="+"zzzz"+";OPTION=3;"
-MySQLhandle=sqlstringconnect(MySQLstr)
-SQLEXEC(MySQLhandle, 'INSERT INTO xx (name,date,memo) VALUES ("Johns","03:04:2005",MEMO)')
IF there is MEMO it works only if it it shortes than 255, or if MEMO i s there not !
Last edited by jlenyi; Jun 30th, 2008 at 12:32 pm.
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
JSP is a new world for me, since 3 days ago. I am having a problem (or I have no idea) how does the inserting datas into database works. I have looked at some examples, but still, im having a hard time. I know PHP and ASP, but JSP is so new for me. Here's part of my codes:
------------------------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%
String firstname = request.getParameter("FIRSTNAME");
String lastname = request.getParameter("LASTNAME");
String address1 = request.getParameter("ADDRESS");
String address2 = request.getParameter("ADDRESS2");
String address3 = request.getParameter("ADDRESS3");
String city = request.getParameter("CITY");
String province = request.getParameter("PROVINCE");
String memo = request.getParameter("MEMO");
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/funeral", "root", "abc123");
ResultSet rs = null;
Statement statement = connection.createStatement();
String sql = ("INSERT INTO memory VALUES ('" + firstname + "','" + lastname + "','" + address1 + "','"+ address2 +"','"+ address3 +"','"+ city +"','"+ postalcode +"','"+ province +"','"+ memo +"') ");
statement.executeUpdate(sql);
rs.close();
connection.close();
%>---------------------------------------------------
Can someone help? Thanks....
------------------------------------------------------------------------------------------------MY CODE-------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head><title>JSP Page</title></head>
<body>
<%
String school = request.getParameter("name");
String name = request.getParameter("username");
String email = request.getParameter("txtEmail");
String pass1 = request.getParameter("pass1");
String pass2 = request.getParameter("pass2");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:shobana", "SYSTEM", "nagalakshmi");
Statement st = con.createStatement();
String sql = ("INSERT INTO register VALUES ('" + school+ "','" + name + "','" + email + "','"+ pass1 +"','"+ pass2+"') ");
st.executeUpdate(sql);
con.close();
%>
connection succssful
</body>
</html>
1
#9 20 Days Ago
•
•
•
•
------------------------------------------------------------------------------------------------MY CODE-------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
<head><title>JSP Page</title></head>
<body>
<%
String school = request.getParameter("name");
String name = request.getParameter("username");
String email = request.getParameter("txtEmail");
String pass1 = request.getParameter("pass1");
String pass2 = request.getParameter("pass2");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:shobana", "SYSTEM", "nagalakshmi");
Statement st = con.createStatement();
String sql = ("INSERT INTO register VALUES ('" + school+ "','" + name + "','" + email + "','"+ pass1 +"','"+ pass2+"') ");
st.executeUpdate(sql);
con.close();
%>
connection succssful
</body>
</html>
And what is the point of this post? This is a 4 year old thread which has been revived 1 year ago, with no useful information. Can someone please close this?
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- insert data from one form into two diffent database table (JSP)
- how to insert data from dataset into database (VB.NET)
- How to insert data in one column in database? (ColdFusion)
- Inserting Data into Access Database (Java)
- Insert Data in to Db from the Dynamic Rows (HTML and CSS)
- Unable to insert data into database. (MySQL)
- Unable to insert data into SQL Database (ASP)
- Posting form data to an Oracle database (JavaScript / DHTML / AJAX)
Other Threads in the JSP Forum
- Previous Thread: javaBean
- Next Thread: How to redirect a page in JSP
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web





