简体   繁体   中英

Java Oracle database connection error

I am new to Java and Oracle. I am trying to make an application that lists serial numbers of a product and when you click on a product's serial number from the list, it shows the other column informations from the database in a textbox. I have a form named CRUD.I am using Oracle 10g. Code is here:

package project1;

import java.sql.Connection;
import java.sql.DriverManager;
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;


public class CRUD extends javax.swing.JFrame {


      Connection connection = null; 
    public CRUD() {

                 try {
              initComponents();
              String driverName = "oracle.jdbc.driver.OracleDriver";
              Class.forName(driverName);
              String serverName = "192.168.0.36";
              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 SERI_NO FROM KART");


       while(rs.next()) // dönebildiği süre boyunca
       {
           String s = rs.getString("SERI_NO") ; //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 SERI_NO in ("+swhere+")") ;
                     String temp="";

                          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);
                   }         
               }
           }
       });



    }

Errors i get are here:

20.Şub.2014 11:22:11 project1.CRUD$1 valueChanged
SEVERE: null
java.sql.SQLSyntaxErrorException: ORA-00904: "SNS080961097": invalid identifier
    .....
    at project1.CRUD$1.valueChanged(CRUD.java:78)
    ...... 

As I said before, I am new to both Java and Oracle. If the errors are so obvious don't laugh:)

Actually there is no problems with you connection step to Oracle DB, its connected successfully, Your problem is within the query. make sure that you have a SERI_NO column in your KART table.

i suggest you to RUN the both same queries in you code from any SQL client such SQLDeveloper and see what these queries retrieve.

Your this query

ResultSet rsx = stmtx.executeQuery("SELECT * FROM KART where SERI_NO in ("+swhere+")") ;

should be like this:

ResultSet rsx = stmtx.executeQuery("SELECT * FROM KART where SERI_NO in ('"+swhere+"')") ;

Observe this statement once,

swhere=swhere.substring(0,swhere.length()-1);

replace the above statement with the following

shere=swhere.substring(0,swhere.length()-2);

Because an extra comma(,) is included in your sql statement.

There is no issue with your connection.

Please add some logging to your code and you will know exactly where the error is throwing.

I guess the error is throwing in this line.. SELECT * FROM KART where SERI_NO in ("+swhere+")

You have to specify this as a string with '',where you actual select should look like below. SELECT * FROM KART where SERI_NO in ('ABC','XCV');

so please check with this one check the value of the "swhere"

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