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);
        }
    }
});
alex2410
  • 10,904
  • 3
  • 25
  • 41
RAJIL KV
  • 399
  • 1
  • 7
  • 24

2 Answers2

3

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

load drivers first

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • 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) – RAJIL KV Dec 25 '13 at 10:09
  • @user3007882 Why are you toggling the Accepted answer ? – Deepak Dec 25 '13 at 11:50
  • @user3007882 Its your choice. – AJ. Dec 25 '13 at 12:04
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

Deepak
  • 2,287
  • 1
  • 23
  • 30