aspose file tools*
The moose likes JDBC and the fly likes How to insert integer variable using jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to insert integer variable using jdbc" Watch "How to insert integer variable using jdbc" New topic

How to insert integer variable using jdbc

indrajit mukherjee

Joined: May 20, 2012
Posts: 1
<%@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 {
con=DriverManager.getConnection(url, user, password);
st= con.createStatement();
catch(Exception ex) {
<%= bid%>

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

Joined: Jun 05, 2010
Posts: 622
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

Joined: Jan 29, 2007
Posts: 313

As Kunal said, separate your persistence logic from your presentation logic.

Moreover, I would prefer PreparedStatement over Statement. It is clean and simple.

Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

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

Joined: Oct 21, 2008
Posts: 1107

but this one might actually help with your problem, use a columns list so insert into table (columns..) values (...)
I agree. Here's the link:
subject: How to insert integer variable using jdbc