2010-09-09 55 views
0
package myfirst; 
import org.apache.poi.hssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*; 
import java.io.FileInputStream; 
import java.lang.Iterable; 
import java.net.URL; 
import java.net.URLConnection; 
import java.sql.*; 

public class ReadExcel { 
public static String fileToBeRead = "C:/Documents and Settings/Developer/Desktop/Anand exmps/Anand.xls"; 
public static void main(String argv[]) { 
String urlcnt=" "; 
Connection con=null; 
Statement stmt=null; 
int i=0; 
int j=0; 

try { 
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); 
    HSSFSheet sheet = workbook.getSheetAt(0); 
    //HSSFRow row = sheet.getRow(0); 
    //HSSFCell cell = row.getCell((short) 0); 
    for (Row row : sheet) { 
    //for (Cell cell : row) { 
     Cell firstCell = row.getCell(0); 
     urlcnt=firstCell.getRichStringCellValue().getString(); 
     System.out.println(urlcnt); 
     if(con==null){ 
       SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK"); 
       con=SQLConnection.getNewConnection(); 
       stmt=con.createStatement(); 
     } 
     try{ 
      ResultSet rs; 
      boolean hasRows=false; 
      rs=stmt.executeQuery("select url from urls_linkins where url='"+urlcnt+"'"); 
      while(rs.next()){ 
       hasRows=true; 
       i++; 
       //String mem=rs.getString(1); 
       rs.close(); 
       //return "This URL already exists in DB"; 
      } 
      if(!hasRows){ 
       j++; 
       PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_linkins(url, source_name, is_active, is_periodic, Link_Type, New_Entry) VALUES(?, ?, ?, ?, ?, ?)"); 
       //insertUrlStatement.setInt(1, 21211); 
       insertUrlStatement.setString(1, urlcnt); 
       insertUrlStatement.setInt(2, 1); 
       insertUrlStatement.setInt(3, 1); 
       insertUrlStatement.setInt(4, 0); 
       insertUrlStatement.setInt(5, 1); 
       insertUrlStatement.setInt(6, 1); 
       insertUrlStatement.executeUpdate(); 
       insertUrlStatement.close(); 
      } 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
      } 
}catch(Exception e){ 
    e.printStackTrace(); 
}finally{ 
    System.out.println(""+j+" url has been added and "+i+" url already exists in the DB"); 
} 
} 
} 

我編了上述程序成功地但在執行相同的,我收到以下錯誤NoClassDefFoundError的:READEXCEL(錯誤的名稱

Exception in thread "main" java.lang.NoClassDefFoundError: ReadExcel (wrong name 
: myfirst/ReadExcel) 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClassCond(Unknown Source) 
    at java.lang.ClassLoader.defineClass(Unknown Source) 
    at java.security.SecureClassLoader.defineClass(Unknown Source) 
    at java.net.URLClassLoader.defineClass(Unknown Source) 
    at java.net.URLClassLoader.access$000(Unknown Source) 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
Could not find the main class: ReadExcel. Program will exit. 

這可怎麼解決

。?按照要求將它作爲myfirst.ReadExcel執行,但現在發現此錯誤。

Exception in thread "main"java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFWorkbook 
    at myfirst.ReadExcel.main(ReadExcel.java:20) 
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook 
    at java.net.URLClassLoader$1.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
    at java.lang.ClassLoader.loadClass(Unknown Source) 
    ... 1 more 
+0

你可以發佈你正在使用的命令運行這個代碼?你在使用IDE還是命令行編譯器? – 2010-09-09 16:35:47

+0

我正在使用命令行編譯器。 「java -cp。; poi-3.6-20091214.jar ReadExcel」這是我用來執行的命令。 – LGAP 2010-09-09 17:13:27

+0

你從哪個目錄運行這個命令? – dogbane 2010-09-09 17:33:12

回答

2

你不應該從myfirst目錄中運行這個。進入上一層,然後運行它:

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\CheckURL\WEB-INF\classes> java -cp .;..\lib\poi-3.6-20091214.jar myfirst.ReadExcel

+0

我已更新我的問題。你能幫忙嗎? – LGAP 2010-09-09 17:43:31

+0

應注意的是,如果是web應用程序,第三方JAR文件通常放在'WEB-INF/lib'文件夾中,所以'-cp'應該讀取'。; ../ lib/poi-3.6-20091214。 jar'。 – BalusC 2010-09-09 17:44:24

+0

WEB-INF中沒有名爲lib的文件夾。 – LGAP 2010-09-09 17:51:01

0

請嘗試java -cp yourotherlib1.jar;yourotherlib2.jar myfirst.ReadExcel而不是java myfirst/ReadExcel

不要忘記在類路徑中包含POI庫及其依賴關係。

編輯:將classpath分隔符從:更改爲;對於Windows。

+0

這樣做,我得到這個錯誤,如上所示。 – LGAP 2010-09-09 16:01:13

+0

我會建議更新您的原始問題與您嘗試和它的輸出。它會更加明顯,而且更具可讀性。 – zigdon 2010-09-09 16:01:47

0

您必須在父文件夾中的「myFirst」文件夾中的和運行

java -cp **dependencies here** myfirst.ReadExcel 
+0

-cp標誌應該在類的名稱之前。 – dogbane 2010-09-09 17:44:58

+0

@fahdshariff是的,它確實...... – 2010-09-09 17:45:31