• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Insert Statement in Java Sql Programming

I am new in Java ODBC-JDBC programming. hence i am facing lot of problems with the database designing and updating.
pls guide me for the following :
 How to write the Dynamic Insert statement and its processing with the existing database. If possible, explain with the code.
0
kpooja
Asked:
kpooja
1 Solution
 
heyhey_Commented:
your question is too general - read the JDBC tutorial first (www.javasoft.com).
0
 
setiawanCommented:
Hi kpooja, I assumed that you have already know how to connect to database server using Java ODBC-JDBC

if you have problem, see :
http://codeguru.developer.com/java/articles/485.shtml

Statement stmt;
 String sql;    
 int rows;      

 sql = "INSERT INTO tCust " 
     + "(custId, custName, custAddr) "
     + "VALUES "
     + "('" + custId   + "',"
     + "('" + custName + "',"
     + "('" + custAddr + "')";

 stmt = theConn.dbConn.createStatement();
 rows = stmt.executeUpdate(sql);
 theConn.dbConn.commit();
 stmt.close();



A PreparedStatement is used to insert data containing QUOTES



 PreparedStatement stmt = null;
 String sql;    
 int rows;      

 try {
   sql = "INSERT INTO tCust"
         + "(custName) "
         + "VALUES "
         + "(?)";
   stmt = theConn.dbConn.prepareStatement(sql);
   stmt.setString(1, "Name with \" are permitted!");
   rows = stmt.executeUpdate();
   theConn.dbConn.commit();
   stmt.close();
   System.out.println(sql);            
   }
 catch (Exception e){
   e,printStackTrace();
   }

taken from : http://codeguru.developer.com/java/articles/487.shtml
0
 
Ravindra76Commented:
Hi kpooja,

I did n't completely understand your goal.

It is static.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

 con = DriverManager.getConnection("jdbc:odbc:yourdsn");


query = "insert into yourtable(yourname) values (?)";

pst = con.prepareStatement(query);
pst.setString(1,s);
int i = pst.executeUpdate();

Dynamic: -

I don't know you requirement.

In dynamic, the query string may contain dynamic ? and correspondingly set statemtns for preparedStatement for thers ?.

Can you tell what exactly you require

Best of luck


0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Ravindra76Commented:
Hi kpooja,

 I did n't see setiawan  comments before answering.

I think it is clear to you know
0
 
rkrubaCommented:
The above comments themselves answers your question..
Please be specific with your question?
So that you can get answers..

All the Best
0
 
heyhey_Commented:
Proposed Answer
From: rkruba
Date: Friday, December 03 1999 - 01:43AM PST
The above comments themselves answers your question..

....
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now