• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

help with getting the table name from the user input

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone i hope someone can help me.
i have this code which is below.
i have a GUI where the user puts in his table names and the columns to be inserted into. for example he would enter 10 diffrent table and column names to insert his text to. but if the user put in the same table name in the GUI.
for example he would type in TextField1 Customers
and in TextField2 he also Typed in customers.
how would i be able test if the table name is the same table name in another prepared statement and if it is use only 1 INSERT statement to insert the text to the 2 columns?

String id = "hello";
String firstname = "james";
String stmp = JTextField1.getText();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO "+stmp+ "([FirstName]) VALUES (?, ?)");

pstmt.setString(1, id);
pstmt.setString(2, firstname);
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest that you add the names your user enters to an ArrayList. Then all you have to do with the next values is use the ArrayList's contains() method to see if it's already in there...if it's true then you know it's been used.

-Pat
[ December 05, 2002: Message edited by: Pat Wallwork ]
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello pat
thanks for the idea.
but what can i do if the user types in the same table name, but names a diffrent column name. for example first of all he names table customers, then names column firstname. then he names table customers again but names a diffrent column name.
how could i get both to work in one INSERT statement. but also he might not name the same table name
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure you can supply a tablename in a prepared statement like that?
I thought Java PreparedStatements (static sql) requires the table name so the db can cache the query plan.
The Java Statement (dynamic sql) can supply the tablename like you suggested.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic