Java-Oracle插入记录

时间:2014-02-27 09:43:58

标签: java oracle jdbc sql-insert

我想从文本字段中插入我的数据库中的记录,但它不起作用。我正在使用Oracle 10g。我是所有这一切的新手。我的整个代码都在这里但是无效的部分是private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)按钮代码:

    package project1;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import static project1.InsertFrame.connection;
    import javax.swing.table.DefaultTableModel;


    public class CRUD extends javax.swing.JFrame {

    //static Connection connection = null; 
         //Connection connection = null; 

          public CRUD() {

                     try {
                  initComponents();
                  String driverName = "oracle.jdbc.driver.OracleDriver";
                  Class.forName(driverName);
                  String serverName = "192.168.0.2";
                  String portNumber = "1521";
                  String sid = "XE";
                  String url = "jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
                  String userName = "HR";
                  String password = "hr";
                         try {
                             connection = DriverManager.getConnection(url,userName,password);
                         } catch (SQLException ex) {
                             Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, ex);
                         }
                      try {
             String temp="";
             Statement stmt = connection.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT DISTINCT URUN_TIPI FROM KART");


           while(rs.next()) // dönebildiği süre boyunca
           {
               String s = rs.getString("URUN_TIPI") ; //kolon isimleri oluşturuldu
               temp+=s+"_";
           }        

           Object [] tem_obj;

           tem_obj=temp.split("_");
           listOgrenciler.setListData(tem_obj);

       } catch (SQLException ex) {
           Logger.getLogger(edit.class.getName()).log(Level.SEVERE, null, ex);
       }

          } catch (ClassNotFoundException ex) {
              Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, ex);
          }    


             listOgrenciler.addListSelectionListener(new ListSelectionListener() {

               @Override
               public void valueChanged(ListSelectionEvent arg0) {
                   if (!arg0.getValueIsAdjusting()) {
                       try {
                         Statement stmtx = connection.createStatement();
                           Object[] sss=listOgrenciler.getSelectedValues();
                         String swhere="" ;
                         for (int i = 0; i < sss.length; i++) {
                               swhere+=sss[i].toString()+",";
                               }
                            swhere=swhere.substring(0,swhere.length()-1);
                         ResultSet rsx = stmtx.executeQuery("SELECT * FROM KART where URUN_TIPI in ('"+swhere+"')") ;
                         String temp="";
                         while(rsx.next()) // dönebildiği süre boyunca
                           {
                               String s = " * "+rsx.getString("BOLGE")+" - "+rsx.getString("SEHIR")+" - "+rsx.getString("LOKASYON")+" - "+rsx.getString("SERI_NO")+" - "+rsx.getString("URUN_TIPI")+" - "+rsx.getString("URUN_KODU")+" - "+rsx.getString("FAZ")+" - "+rsx.getString("SEVK_TARIH")+" - "+rsx.getString("ACIKLAMA")   ; //kolon isimleri oluşturuldu
                               temp+=s+"_";
                           }        

                             Object [] tem_obj;
                             tem_obj=temp.split("_");
                           String ara="";
                             for (int i = 0; i < tem_obj.length; i++) {
                               ara+=tem_obj[i].toString()+"\n";
                             }
                            texttoarea.setText(ara);

                       } catch (SQLException ex) 
                       {
                           Logger.getLogger(edit.class.getName()).log(Level.SEVERE, null, ex);
                       }         
                   }
               }
           });



        }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        try{

      String sqlInsert="INSERT INTO KART VALUES (?,?,?,?,?,?,?,?,?,?)";

      PreparedStatement psta=connection.preparedStatement(sqlInsert);
      psta.setString(1, jTextField1.getText());
      psta.setString(2, jTextField2.getText());
      psta.setString(3, jTextField3.getText());
      psta.setString(4, jTextField4.getText());
      psta.setString(5, jTextField5.getText());
      psta.setString(6, jTextField6.getText());
      psta.setString(7, jTextField7.getText());
      psta.setString(8, jTextField8.getText());
      psta.setString(9, jTextField9.getText());
      psta.setString(10, jTextField10.getText());

      psta.execute();
      psta.close();
      }
      catch(Exception e)
              {
              System.out.println(e.getCause());
              }
    } 
 public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CRUD().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollBar jScrollBar2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.swing.JList listOgrenciler;
    private javax.swing.JTextArea texttoarea;
    // End of variables declaration                   
}

我无法添加照片,但我的记录包含数字和字母,如“SNS112468698”和“08ARTVIN_IL”。它说无法找到符号preparedStatement.I将其更改为psta但它不起作用。问题是什么?非常感谢你。

1 个答案:

答案 0 :(得分:0)

cannot find symbol symbol:
method preparedStatement(String)
location: variable connection of type Connection

该方法称为prepareStatement,而非preparedStatement(请注意d)。

另请参阅java.sql.Connection.prepareStatement() Javadoc.