It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes help with a sql statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "help with a sql statement" Watch "help with a sql statement" New topic

help with a sql statement

ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
hello everyone i hope someone can help me.
i have this code which is below.
what the code is doing is testing to see if any other strings are equal to st1 and if they are get there values from the hashtable.(which is ok)
But now i want set a sql statement so that the keys of all the values that are equal to st1 are put in the sql statement as column names but as you can see i am getting a bit stuck with my code could someone please help me?
thanks for yr time
import java.util.*;
import java.lang.*;
import java.sql.*;
public class has21 {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
Hashtable hash = new Hashtable();
String a = "firstname"; //column names
String b = "lastname";
String c = "id";
String d = "adress1";
String k = "phone";
String f = "county";
String g = "post code";
String h = "country";
String i = "goods in";
String j = "goods out";
String st1 = "ben";
String st2 = "ben"; //table names
String st3 = "alex";
String st4 = "alex";
String st5 = "matt";
String st6 = "matt";
String st7 = "mum";
String st8 = "mum";
String st9 = "dad";
String st10 = "ben";
hash.put(a, st1);
hash.put(b, st2);
hash.put(c, st3);
hash.put(d, st4);
hash.put(k, st5);
hash.put(f, st6);
hash.put(g, st7);
hash.put(h, st8);
hash.put(i, st9);
hash.put(j, st10);
if(hash.containsValue(st1)) {
Enumeration e = hash.keys();
while (e.hasMoreElements()) {
String tempkey = (String) e.nextElement();
String tempvalue = (String) hash.get(tempkey);
if(tempvalue.equals(st1)) {
try {
String data = "jdbc dbc:myProject";
Connection con = DriverManager.getConnection(data,"","");
String x = "Insert INTO st1"(
for (int t = 0; t < arr.size(); t++){
x = x + "," VALUES(?, ?, ?)")";
PreparedStatement prepStmt = con.prepareStatement(x);
prepStmt.setString(1, "hello");
prepStmt.setString(2, "hi");
prepStmt.setString(3, "bye");
} catch (Exception e1) {
System.err.println("Got an exception! ");

I agree. Here's the link:
subject: help with a sql statement
It's not a secret anymore!