• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert integer variable using jdbc

 
indrajit mukherjee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@page import="java.sql.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
String bids = request.getParameter("bid");
int bid=Integer.parseInt(bids);
String drafts=request.getParameter("draft");
float draft=Float.parseFloat(drafts);
String lengths= (request.getParameter("length"));
float length= Float.parseFloat(lengths);
String cargo_type=request.getParameter("cargo_type");
String vessel_type=request.getParameter("vessel_type");
String etas= request.getParameter("eta");
float eta=Float.valueOf(etas);
//Date eta=Date.valueOf(etas);
String etds= request.getParameter("etd");
Float etd=Float.valueOf(etds);
//Date etd=Date.valueOf(etds);
String vcn=request.getParameter("vcn");
%>
<%
String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mysql";
String user="root";
String password="root";
Connection con;
Statement st;
String sql="insert into test.berman values(bid,draft,length,'"+cargo_type+"','"+vessel_type+"',eta,etd,'"+vcn+"')";
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
st= con.createStatement();
st.executeUpdate(sql);
}
catch(Exception ex) {
out.println(ex);
}
%>
<html>
<%= bid%>
</html>

========================
It inserted row in the table but in place of ineger variable(bid) & float variable(draft,length,eta,etd) null inserted which are actually non-null. But I printed it those variable in html and found that all are non-null.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use MVC architecture. Make a separate class for persistence logic and use jsp to render the view

Apart form integer and float values, all string are properly inserted???
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Kunal said, separate your persistence logic from your presentation logic.

Moreover, I would prefer PreparedStatement over Statement. It is clean and simple.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three things:

ONE: like the other posters said, you should not be doing database connections or queries inside a jsp. There is now a FAQ as to why: WhyNotUseScriptlets
TWO: Please format your code by using Code Tags. It makes it easier to read

THREE: As Palak Mathur said, PreparedStatement is better. Your "statement" as it is is a big security risk because it is open to SQL injection.
 
Wendy Gibbons
Bartender
Posts: 1110
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but this one might actually help with your problem, use a columns list so insert into table (columns..) values (...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic