简体   繁体   中英

How to insert data into oracle database from JTextfield?

I have a Swing application connected to an oracle database. I'm trying to insert the data input from the JTextfields into my database (when the 'save' button is clicked).
I have read similar posts and tried the following code but it doesn't work. Any help would be appreciated! Thanks!

EDIT:
I get the error:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

I have the ojdbc6.jar file

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String lastName = txtlastName.getText(),
           firstName = txtlastName.getText(),
           ContactNumber = txtContactNumber.getText(),
           Email = txtEmail.getText();
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","pw");
        Statement stmt=con.createStatement();

        if(con!=null) {
            System.out.println("Success!");
        }
        else {
            System.out.println("Failed!");
        }

        String sql = "Insert Into Customer values(CUSTIDSEQ.NEXTVAL,+'"+lastName+"','"+firstName+"','"+ContactNumber+"','"+Email+"')";
        Statement st=con.createStatement();
        int n=st.executeUpdate(sql);
        //System.out.println(n+" "+"record inserted");

        if (n==1) {
            JOptionPane.showMessageDialog(this,"Record inserted");
        }
        else {
            JOptionPane.showMessageDialog(this,"Record not inserted");
        }

        con.close();

    } catch(Exception e) {System.out.println(e);
}
 Class.forName("oracle.jdbc.driver.OracleDriver");

This line causes ClassNotFoundException, because you haven't placed ojdbc14.jar file in your lib folder of the project. or You haven't set the classpath of the required jar.

It seems to me that your jar is not being taken into account in your project. Try to put it in your lib folder or in the jre\\lib\\ext folder of the Java version you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM