Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to insert data from jsp form page to database

 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

.
 
Radhakrishna Sharma Gorenta
Ranch Hand
Posts: 63
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. take the java code in a separate class. and invoke that from your JSP
 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic