4

I have a java Desktop application. I am trying to insert data from two text field in to Database. But getting some runtime error. Help me to resolve it.

Here is my code snippet

final String s1 = t1.getText();
final String s2 = t2.getText();
jb.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        String host = "jdbc:derby://localhost:1527/Details";
        String uName = "rajil";
        String uPass = "rajil";
        try {
            Connection con = DriverManager.getConnection(host, uName,uPass);
            Statement st = con.createStatement();
            String q1 = "insert into name (name,id) values('" + s1 + "','" + s2 + "')";
            st.executeQuery(q1);
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
});
5
  • what error you are getting ? Commented Dec 25, 2013 at 10:00
  • and where is an error? Commented Dec 25, 2013 at 10:00
  • java.sql.SQLException: executeQuery method can not be used for update. at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) at org.apache.derby.client.am.Statement.executeQuery(Unknown Source) Commented Dec 25, 2013 at 10:03
  • there is no syntax error .its runtime Commented Dec 25, 2013 at 10:04
  • Don't put stack traces in comments. Instead, edit the question.. Commented Dec 25, 2013 at 10:17

2 Answers 2

3

change st.executeQuery(q1); to st.executeUpdate(q1);

load drivers first

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Sign up to request clarification or add additional context in comments.

3 Comments

okay .But after getting another error java.sql.SQLSyntaxErrorException: Table/View 'DTS' does not exist. at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) at org.apache.derby.client.am.Statement.executeUpdate(Unknown Source)
@user3007882 Why are you toggling the Accepted answer ?
@user3007882 Its your choice.
1

You have to use below statement also for loading the Drivers classes

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();

For eg mysql

Class.forName("com.mysql.jdbc.Driver");

See the below link for detail

http://www.vogella.com/articles/ApacheDerby/article.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.