java.io.FileNotFoundException:系统找不到-while数据库
代号插入图像指定的文件:java.io.FileNotFoundException:系统找不到-while数据库
package com;
import java.io.*;
import java.sql.*;
public class Sample {
public static void main(String a[]){
Connection con = null;
PreparedStatement ps = null;
InputStream is = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@172.26.132.40:1521:orclilp","aja60core","aja60core");
ps = con.prepareCall("insert into SAMPLE values (?,?,?)");
ps.setString(1, "Himanshu");
ps.setString(2, "Gupta");
is = new FileInputStream(new File("ajax-logo1.jpg"));
ps.setBinaryStream(3, is);
int count = ps.executeUpdate();
System.out.println("Count: "+count);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(is != null) is.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch(Exception ex){}
}
}
}
而且我发现了以下错误:
java.io.FileNotFoundException: ajax-logo1.jpg (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.Sample.main(Sample.java:19)
的FileNotFoundException java解释器找不到文件时发生异常
Signals that an attempt to open the file denoted by a specified pathname has failed.
This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
确保fil E在您的应用程序中存在(其中的.class文件,而不是在.java源文件)
那是一个常见的错误
我已经把该图像与.class文件,但它仍然不是工作 –
你可以发布你的项目的文件结构吗? – MaVRoSCy
In eclipse src - > com(package) - > Sample.java + ajax-logo1.jpg bin - > com(package) - > Sample.class + ajax-logo1.jpg –
当您简单地编写文件的名称(如您在代码示例中所做的那样)时,Java将在您的项目的根目录中查找该文件,即如果您的项目文件位于目录databaseProject
和目录结构是这样的:
`databaseProject
|->src
|->pom.xml (for maven builds)
... `
等 Java程序,默认情况下,会考虑你的工作目录是databaseProject
和查找文件,除非你给出一个绝对或相对(从PWD)路径到你的文件。
一个好的方法是将所有的图像放在一个文件夹中,例如
`databaseProject
|->src
|->images
|->ajax-logo1.jpg
|-> ....
|->pom.xml (for maven builds)
... `
然后使用路径:"/images/<image_name>"
来访问图像。
这也不起作用 –
什么是不工作,把文件放在一个单独的目录?或将其放置在项目的根目录中? –
重复http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists – Satya