File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to insert data from jsp form page to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to insert data from jsp form page to database" Watch "how to insert data from jsp form page to database" New topic
Author

how to insert data from jsp form page to database

Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 72
this is a jsp file . look at the line with code-> Statement st=con.createStatement(); in the below example. the error is "cannot convert from java.sql.Statement to com.mysql.jdbc.Statement
please help me with it. thanks




<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.mysql.jdbc.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="com.mysql.jdbc.Statement"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String t=request.getParameter("title");
String n=request.getParameter("name");
String cn=request.getParameter("cname");
String pos=request.getParameter("pos");
String req=request.getParameter("req");
String eid=request.getParameter("emailId");
String ph1=request.getParameter("phone1");
String ph2=request.getParameter("phone2");



Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/commondb", "root", "root123");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into contactinfo(title,name,cname,pos,req,emailId,phone1,phone2) values('"+t+"','"+n+"','"+cn+"','"+pos+"','"+req+"','"+eid+"',"+ph1+","+ph2+")");
out.println("Data is successfully inserted!");
//System.out.println("rows affected by insert "+st.executeUpdate(i));
}
catch(Exception e){
System.out.print(e);
e.printStackTrace();
}

%>
</body>
</html>
lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
Posts: 130
Can you please insert userdefine Statement class code (com.mysql.jdbc.Statement).

Can you try with below Code instead of user define Connection and statement
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 72
lokesh pattajoshi wrote:Can you please insert userdefine Statement class code (com.mysql.jdbc.Statement).

Can you try with below Code instead of user define Connection and statement
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>


ya tried. still the error persists...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061
    
  21

It's really, really bad practice to put application logic into JSPs and our local Defender of the Faith will probably be along to chastise you soon.

It's also really, really bad practice to build up SQL requests from string concatenations. That's precisely how Sony got eaten.

It's not even a good idea to create connections by brute force in J2EE webapps, as far as that goes. Pooled connections are usually more efficient.

But the fatal offence here is that you shouldn't be using the mysql-specific version of "Statement" class. The only time you ever should use a DBMS vendor's proprietary classes in JDBC is if you have no choice in order to access a vendor-specific feature. Normally a class like com.mysql.jdbc.Statement implements the java.sql.Statement interface and that's the touch-point you should be using.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Tim Holloway wrote:It's really, really bad practice to put application logic into JSPs and our local Defender of the Faith will probably be along to chastise you soon.

.


Learning and Learning!-- Java all the way!
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 50

yes. take the java code in a separate class. and invoke that from your JSP


Regards,

RK
Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 72
solved the issue. i had to press ctrl+space(in eclipse) on the error code and sent it to java.sql.Statment ans some other suff. thanks all for replying.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to insert data from jsp form page to database