NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
zno
Joined: 19 Apr 2010 Posts: 1
|
Posted: Mon Apr 19, 2010 7:55 pm Post subject: SQL query will not be permanently stored in database |
|
|
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 ?
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.
| 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;
}
|
|
|
| Back to top |
|
 |
Oyashiro
Joined: 08 Apr 2010 Posts: 21
|
Posted: Fri Apr 23, 2010 4:17 pm Post subject: |
|
|
| Since you disabled the auto-commit, try to commit the changes before closing the connection. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|