• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

CREATE table upon button click in eclipse

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a sql statement into my eclipse code that creates/recreates a table when the 'intialize database' button is clicked. I believe my create statement is correct, but I am receiving no response when the button is clicked. Is there something wrong with my statement? Or am I missing something else in the code?


 
Marshal
Posts: 26114
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest extracting the database code from that program and writing a small program which does nothing but run the database code. It's a lot easier to test code which isn't encumbered by a lot of other unrelated code.

Let us know if you still have problems!
 
Marshal
Posts: 3258
487
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try using your same DDL command directly with the mysql command-line interface and see what happens there.

Also, visually inspect the command to confirm everything looks correct.
 
Taylor Manley
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
I now have the database create statements by themselves and linked to the interface with the button. One more question, since the tables are created once the button is pressed every button press thereafter throws the error "table already exists" is there a way to do away with this or have the table be recreated every time the button is pressed?

In case you need to see my code for the database now

 
Rancher
Posts: 4740
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to that is a case of what do you want it to do if the table already exists?

If it should DROP the existing table and recreate it then do that (if losing all the data that's in there is OK).
If it should ignore the error then do that.
If it should prompt the user then do that.

But you need to decide what it should do.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you've implemented what Dave Tolls suggested, try cleaning up the code.

* Try commenting out line 18, (Class.forName...)  I'd be surprised if you need it.  Now you don't need to catch Exception.
* Separate the one giant try/catch block into two blocks, one that catches the connection errors and one that catches the statement errors.
* Now you can use try with resources instead of the finally blocks.
* Format your code correctly.  Follow this link for HowToFormatCode.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statement str = con.createStatement();
String sql = "create table "+textfield.getText()+
               " (ID VARCHAR(40) not NULL, "+
               " NAME VARCHAR(50), "+
               " SECTION VARCHAR(10), "+
               " PRIMARY KEY (ID))";
str.executeUpdate(sql);

I did this with a textfield and a button so I can type the table name that I want. Hope this help.
 
Marshal
Posts: 71028
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds good, MM, and welcome to the Ranch
 
I will suppress my every urge. But not this shameless plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic