NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

SQL query will not be permanently stored in database

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
zno



Joined: 19 Apr 2010
Posts: 1

PostPosted: Mon Apr 19, 2010 7:55 pm    Post subject: SQL query will not be permanently stored in database Reply with quote

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.

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

PostPosted: Fri Apr 23, 2010 4:17 pm    Post subject: Reply with quote

Since you disabled the auto-commit, try to commit the changes before closing the connection.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo