Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将图像插入数据库代码无效_Java_Runtime Error_Execution_Classnotfoundexception - Fatal编程技术网

Java将图像插入数据库代码无效

Java将图像插入数据库代码无效,java,runtime-error,execution,classnotfoundexception,Java,Runtime Error,Execution,Classnotfoundexception,正如您所看到的,我正在执行java文件的当前路径中。 当我编译程序时,我成功地得到了一个类文件。 但是当我运行它时,我无法执行该程序,因为我在图像中得到一个错误,告诉我在该路径中没有这样的类 C:\Users\admin>javac insertImg.java C:\Users\admin>java insertImg Exception in thread "main" java.lang.NoClassDefFoundError: insertImg Caused by: j

正如您所看到的,我正在执行java文件的当前路径中。 当我编译程序时,我成功地得到了一个类文件。 但是当我运行它时,我无法执行该程序,因为我在图像中得到一个错误,告诉我在该路径中没有这样的类

C:\Users\admin>javac insertImg.java

C:\Users\admin>java insertImg
Exception in thread "main" java.lang.NoClassDefFoundError: insertImg
Caused by: java.lang.ClassNotFoundException: insertImg
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: insertImg.  Program will exit.

C:\Users\admin>

这是我将图像插入数据库的java代码。。。。。 insertImg.java:

import java.sql.*;
import java.io.*;

public class insertImg{
    public static void main(String[] args) {

        System.out.println("Insert Image Example!");
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "hibernatetutorial";
        String userName = "root";
        String password = "root";
        Connection con = null;

        try {
            Class.forName(driverName);
            con = DriverManager.getConnection(url+dbName,userName,password);
            Statement st = con.createStatement();

            File imgfile = new File("images.jpg");
            FileInputStream fin = new FileInputStream(imgfile);

            PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
            pre.setInt(1,5);
            pre.setString(2,"Durga");
            pre.setBinaryStream(3,fin,(int)imgfile.length());
            pre.executeUpdate();

            System.out.println("Inserting Successfully!");

            pre.close();
            con.close();  

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
} 

insertImg.java是否有一个类insertImg(注意区分大小写),它有一个公共的
main
函数?

我看到你从roseIndia那里得到了代码,它对我来说没有任何问题。然而,无论你展示什么,它仍然很奇怪。我的建议是使用像eclipse这样的IDE

您始终可以尝试以下步骤

1) 在Mysql数据库中创建一个表以适应上述代码

CREATE TABLE IMAGE
(
 IMG_ID    INT,
 IMG_NAME  VARCHAR2(100),
 IMG       BLOB
)
2) 在eclipse中创建一个新的java项目,创建一个新的类insertImg并将代码粘贴到其中。请确保图像文件路径与图像所在位置相同,然后运行它。它工作正常

我试过使用Oracle数据库,但不得不添加-classpath ojdbc.jar,而且它在控制台上也能正常工作,如图所示 我在数据库中加载的图像文件位于D:\中,因此它不在上面的屏幕截图中


希望这有帮助这是一个简单的类路径错误。
CLASSPATH
环境变量允许Java知道在哪里查找类

不幸的是,Java不够智能,无法知道它需要在本地目录中查找类。要解决此问题,您只需执行以下语句之一,具体取决于您使用的是windows还是Linux/Unix:

对于Windows:

set CLASSPATH=.;%CLASSPATH%
对于Linux:

export CLASSPATH=.:$CLASSPATH

发布代码如何加载图像…murali和stefan我已经发布了上面的代码…你能识别我代码中的一些问题吗…如果是,请建议…生成的类文件是否移动到其他目录?如果是,,然后你需要在该目录中执行
java insertImg
,或者设置类路径以包含该文件夹。试试javac-d insertImg.javayeah一切似乎都很完美…但我不知道问题出在哪里我创建了同一个表,甚至使用了mysql连接器来支持jdbc连接…仍然有同样的问题…我会的按照您的第二步,然后返回给您……它在eclipse ide中运行良好……但出于什么原因,同一个程序在非基于ide的环境中不工作……您得到了一个classnot found异常,但我得到了一个not class definition found异常……我甚至通过添加所有必要的jar文件(如ojdbc和mysql)进行了尝试connector.jar文件…但似乎有其他东西引发了该错误…您的图像文件在哪里…我在那里看不到任何东西…我无法重建您的错误,但它在控制台中对我有效,或者指定
-cp
参数。