Question

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);
        }
    }
});
Was it helpful?

Solution 2

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

OTHER TIPS

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

load drivers first

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top