Results 1 to 12 of 12
  1. #1
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default entering textfile into database

    I am facing problem in loading connector.Please help me.
    Here is my code....

    import java.io.File;
    import java.io.FileInputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;

    public class InsertTextFileToMySQL {

    public static Connection getConnection() throws Exception {
    String driver = "com.mysql.JDBC.Driver";
    String url = "jdbc:mysql://localhost/myfiles";
    String username = "root";
    String password = "123456";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
    }

    public static void main(String[] args)throws Exception {
    String id = "001";
    String fileName = "textfile.txt";

    FileInputStream fis = null;
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
    conn = getConnection();
    conn.setAutoCommit(false);
    File file = new File(fileName);
    fis = new FileInputStream(file);
    pstmt = conn.prepareStatement("insert into DataFiles(id, fileName, fileBody) values (?, ?, ?)");
    pstmt.setString(1, id);
    pstmt.setString(2, fileName);
    pstmt.setAsciiStream(3, fis, (int) file.length());
    pstmt.executeUpdate();
    conn.commit();
    } catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
    e.printStackTrace();
    } finally {
    pstmt.close();
    fis.close();
    conn.close();
    }
    }
    }





    After run it occurs errors and displays...

    Error: com.mysql.JDBC.Driver
    java.lang.ClassNotFoundException: com.mysql.JDBC.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at InsertTextFileToMySQL.getConnection(InsertTextFile ToMySQL.java:15)
    at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:28)
    Exception in thread "main" java.lang.NullPointerException
    at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:42)
    Java Result: 1

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    java.lang.ClassNotFoundException: com.mysql.JDBC.Driver
    String driver = "com.mysql.JDBC.Driver";
    ...
    Class.forName(driver);
    You need the class refered to in the error message on the classpath so the JVM can find it.
    It's probably in a jar file. Find that jar file and add it to the classpath

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I am using Netbean 6.8.Please tell me about the JDBC driver.

    //String driver="com.MySQL.JDBC.driver"//
    Give an explanation about this.I am trying but it's not working.Where i will find it?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    String driver = "com.mysql.JDBC.Driver";
    Where did you get the name of the class referenced above?
    You can probably use Google to find the parts you need.

    Class.forName() loads the class and causes some static code in that class to do what needs to be done for your code to work.

  5. #5
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I have found "com.mysql.jdbc.Driver".
    Now,please tell me that where should I store It?
    As I am new to java,please explain.

    Thanks in advance..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    I assume that the drive is in a jar file. Add that jar file to the classpath for the java command.

  7. #7
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Path of my Netbeans 6.8 is....
    C:\Program Files\Java\NetBeans 6.8\bin

    Path of my code is....
    C:\Documents and Settings\UJJAL SUTTRA DHAR\JavaLibrary1\src

    Please tell me what is the path for that .jar file?
    I am trying,but its not working.Help me..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    An example of putting a jar file on the classpath for the java command:
    java -classpath somejarfile.jar ProgramName

    I have no idea of how to make netbeans do this.

  9. #9
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Previous problem is solved.Thanks to everyone.Now I have facing a new one.

    this errors shows...


    Error: textfile.txt (The system cannot find the file specified)
    java.io.FileNotFoundException: textfile.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:31)
    Exception in thread "main" java.lang.NullPointerException
    at InsertTextFileToMySQL.main(InsertTextFileToMySQL.j ava:42)
    Java Result: 1




    My question is where should I place the .txt file.??

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Are you inside an IDE or outside?
    If outside, put the file in the same folder with the class files.
    If inside ???

  11. #11
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I know that.But I am using Netbeans 6.8.Where should i keep this .txt file to read from?

    Please help me.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Sorry, I don't know about how to use Netbeans.
    Try asking on a Netbeans users forum.

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 07:38 AM
  2. Entering a space as argument
    By Arne Bjarne in forum Eclipse
    Replies: 1
    Last Post: 03-02-2009, 12:43 PM
  3. Entering string in a loop with method
    By Konex in forum New To Java
    Replies: 3
    Last Post: 03-01-2009, 07:33 AM
  4. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 07:10 AM
  5. How to stop entering Data
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •