• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to add program arguments to database

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to add program arguments to database

I had created pankaj.java file in which I created database connection and stores values to ABC table. But instead of inserting hardcoded valus to table I want to insert program arguments that I will supply through command line. Anyone knows how to do this. I am using MySQL 4.1.

The code for pankaj.java

***************************************************************

import java.sql.*;

public class pankaj{
public static void main(String args[]){
try {
Statement stmt;
Connection con;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/mysql";
con =DriverManager.getConnection(url,"root", "pankaj");
stmt = con.createStatement();
stmt.executeUpdate("CREATE DATABASE MyDB2");
stmt.close();
con.close();
url ="jdbc:mysql://localhost:3306/MyDB2";
con =DriverManager.getConnection(url,"root", "pankaj");
stmt = con.createStatement();
String createLogin = "CREATE TABLE ABC " +
"(LASTNAME VARCHAR(254), FIRSTNAME VARCHAR(254))";
stmt.executeUpdate(createLogin);

String str1 = args[0];
String str2 = args[1];

stmt.executeUpdate("INSERT INTO ABC VALUES ('shinde', 'pankaj')");

rs = stmt.executeQuery("SELECT * from ABC");
System.out.println("Display all results:");
while(rs.next()){
String last = rs.getString("LASTNAME");
String first = rs.getString("FIRSTNAME");
System.out.println(" " + last + "\t " + first);
}
stmt.executeUpdate("DROP DATABASE MyDB2");
stmt.close();
con.close();
}catch( Exception e ) {
e.printStackTrace();
}
}
}

***************************************************************

Please tell me how to write
stmt.executeUpdate("INSERT INTO ABC VALUES ('shinde', 'pankaj')");
so that i can insert program arguments to database.

Regards

Pankaj Shinde
SCJP 1.4
[ February 27, 2008: Message edited by: Pankaja Shinde ]
 
Rancher
Posts: 43009
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way would be something like



The better way would be to use a PreparedStatement, which performs parameter checking, and also deals with the single quotes around strings:


[ February 27, 2008: Message edited by: Ulf Dittmer ]
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you sir.

Pankaj Shinde
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!