简体   繁体   English

Java Oracle数据库连接错误

[英]Java Oracle database connection error

I am new to Java and Oracle. 我是Java和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. 我有一个名为CRUD的表格。我正在使用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. 如前所述,我对Java和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. 实际上,您与Oracle DB的连接步骤没有问题,它已成功连接,您的问题出在查询内。 make sure that you have a SERI_NO column in your KART table. 确保您的KART表中有一个SERI_NO列。

i suggest you to RUN the both same queries in you code from any SQL client such SQLDeveloper and see what these queries retrieve. 我建议您从任何SQL客户端(例如SQLDeveloper运行代码中的两个相同查询,并查看这些查询检索的内容。

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. 因为您的sql语句中包含一个额外的逗号(,)。

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+") 我猜错误是在此行中引发的。.SELECT * FROM KART其中SERI_NO在(“ + 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'); SELECT * FROM KART其中SERI_NO('ABC','XCV');

so please check with this one check the value of the "swhere" 因此,请检查此一项,检查“ swhere”的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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