• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to insert integer variable using jdbc

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 622
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but this one might actually help with your problem, use a columns list so insert into table (columns..) values (...)
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic