Results 1 to 6 of 6
- 04-20-2010, 08:54 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 8
- Rep Power
- 0
SQL query won't stick in the database :confused:
Hi!
I'm using Netbeans 6.8 and my MySQL connector is working fine. And when I do create my tables and consequently read them it seems to be working like a charm, however, when I try to read them again they're not there? I check manually in the MySQL command prompt and the tables are there, but they're empty ? :confused:
So what I've done is I delete the tables and create them every single time I need to make a query, this works since my database is tiny. But I really would like to get this working how it's supposed to.
Thank you in advance!
Java Code:public ArrayList<String> getQuery(String query){ Connection con = null; Statement st = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/"; String db = "databasename"; String driver = "com.mysql.jdbc.Driver"; String user = "username"; String pass = "password"; try { Class.forName(driver); con = DriverManager.getConnection(url + db, user, pass); con.setAutoCommit(false);// Disables auto-commit. st = con.createStatement(); st.executeUpdate("DROP table components"); st.executeUpdate("DROP table accessories"); st.executeUpdate("create table components(objekt varchar(50),childID int not null)"); st.executeUpdate("create table accessories(ID int not null auto_increment primary key,child varchar(50))"); for(n=1;n<=12;n++){ st.executeUpdate("INSERT INTO accessories VALUES('"+n+"','"+accessories[n-1]+"')"); } st.executeUpdate("INSERT INTO components VALUES('Dörr','1')"); st.executeUpdate("INSERT INTO components VALUES('Dörr','2')"); st.executeUpdate("INSERT INTO components VALUES('Dörr','3')"); st.executeUpdate("INSERT INTO components VALUES('Dörr','4')"); st.executeUpdate("INSERT INTO components VALUES('Dörr','5')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','6')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','7')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','1')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','2')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','8')"); st.executeUpdate("INSERT INTO components VALUES('Fönster','9')"); st.executeUpdate("INSERT INTO components VALUES('Vägg','8')"); st.executeUpdate("INSERT INTO components VALUES('Vägg','10')"); st.executeUpdate("INSERT INTO components VALUES('Vägg','4')"); st.executeUpdate("INSERT INTO components VALUES('Tak','11')"); st.executeUpdate("INSERT INTO components VALUES('Tak','4')"); st.executeUpdate("INSERT INTO components VALUES('Tak','8')"); st.executeUpdate("INSERT INTO components VALUES('Golv','8')"); st.executeUpdate("INSERT INTO components VALUES('Golv','11')"); st.executeUpdate("INSERT INTO components VALUES('Golv','12')"); st.executeBatch(); String q = "SELECT child FROM accessories INNER JOIN components ON components.objekt='"+query+"' AND components.childID=accessories.ID"; rs = st.executeQuery(q); while (rs.next()) { answer.add(rs.getString(1).toString()); } rs.close(); st.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } return answer; }
- 04-20-2010, 10:32 PM #2
Where do you commit your statements?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 04-20-2010, 10:53 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 8
- Rep Power
- 0
I'm sorry I think you're going to have to elaborate. I commit my SQL statements from that method "getQuery", I also opened the MySQL command prompt and wrote select * from components for example. I don't think I understand your question at all, sorry.
- 04-21-2010, 07:25 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No, you execute them there, you do not commit them there. You never call commit() on the connection and you never check whether "autocommit" is true or not, so again, where are you commiting your actions.
Edit: And close your resources in a Finally block unless you want to eventually sabotage your programs ability to use the database (and possibly anyone else, as well).
- 04-21-2010, 08:46 PM #5
Member
- Join Date
- Aug 2008
- Posts
- 8
- Rep Power
- 0
You were absolutely correct, I did not commit at all. I didn't know I had to, but now I do :)
I've added a con.commit() and moved the close() of my resources as you call it into a finally block.
Thanks a lot :)
- 04-22-2010, 09:05 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Query in database
By anki1234 in forum JDBCReplies: 3Last Post: 08-20-2008, 05:45 PM -
Read a Public Key from a USP stick
By shauom in forum New To JavaReplies: 2Last Post: 08-06-2008, 07:57 AM -
How to query data from database using SSL
By mano in forum New To JavaReplies: 0Last Post: 08-02-2007, 05:30 PM -
Displaying different columns with database query
By jennybgh in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-02-2007, 05:04 PM -
confused with protected qualifier.....plz solve this query...
By money123 in forum New To JavaReplies: 3Last Post: 07-30-2007, 07:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks