The Artima Developer Community
Sponsored Link

Java Answers Forum
help with a sql statement

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

help with a sql statement Posted: Dec 16, 2002 9:00 AM
Reply to this message Reply
Advertisement
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
ben
import java.io.*;
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)) {
arr.add(tempkey);

try {
String data = "jdbc:odbc:myProject";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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");
prepStmt.executeUpdate();
prepStmt.close();

} catch (Exception e1) {
System.err.println("Got an exception! ");
System.err.println(e1.getMessage());
}


}

}
}
}
}

Topic: printing an array in a text field Previous Topic   Next Topic Topic: Wanting general help on my current project

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use