Results 1 to 11 of 11
  1. #1
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default preparedStatement

    Hello,

    I am trying to insert data into a db using preparedStatement, because I need to insert a .class file as well. The thing is, connect.prepareStatement doesn't really work. It doesn't insert anything. Before preparedStatement, the following insertion worked fine:

    int up = statement.executeUpdate("INSERT INTO javaFiles (className, classScope, package, superclassName) " + "VALUES ('" + name + "', '" + scope + "', '" + jclassPackage + "', '" + sclass + "')");

    Below, insertion doesn't work. Any help?

    FileInputStream fis = null;
    PreparedStatement ps = null;
    File file = new File(nameOfClass);
    FileInputStream is = new FileInputStream(file);

    ps=connect.prepareStatement("INSERT INTO JavaFiles (className, classScope, package, superclassName, classFile) VALUES (?,?,?,?,?)");
    ps.setString(1, name);
    ps.setString(2, scope);
    ps.setString(3, jclassPackage);
    ps.setString(4, sclass);
    ps.setBinaryStream(5, is, (int)file.length());


    // nameOfClass is the full path where the file exists in the file system, with the file name. i.e. C:/Users/Andreas/Documents/NetBeansProjects/myThesis/Components/Users/bin/User.class
    Last edited by axenos; 03-15-2011 at 11:11 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You'll need to give us some more code, and show us how you are handling exceptions, so we can see if there's something obvious.
    For all we know, from what you've given us, you aren't even running that statement.

  3. #3
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Here is the code of the whole class. Thanks for your reply.


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import org.apache.bcel.classfile.ClassParser;
    import org.apache.bcel.classfile.JavaClass;

    /**
    * Class JavaFiles - Storage of the data of java file that
    * belongs to the component, into the DB.
    *
    * @author Andreas Xenos
    * @version 2011.03.15
    */
    public class JavaFiles {

    private String jclassPackage;
    private String name;
    private JavaClass clazz;
    private String scope;
    private JavaClass[] jclassInter;
    private String interfaceScope;
    private String sclass;

    /**
    * Create a JavaFile described "nameOfClass, connect, statement".
    *
    * @param nameOfClass The name of the class.
    * @param connect Connection to the database.
    * @param statement SQL statement to be executed.
    */
    public JavaFiles(String nameOfClass, Connection connect, Statement statement) throws SQLException, FileNotFoundException {

    FileInputStream fis;
    PreparedStatement ps;
    File file = new File(nameOfClass);
    FileInputStream is = new FileInputStream(file);

    try{
    clazz = new ClassParser(nameOfClass).parse();
    //name of the class
    name = clazz.getClassName();
    if(name.startsWith(clazz.getPackageName())) {
    name = name.substring(clazz.getPackageName().length()+1,n ame.length());
    }
    System.out.println("Name of the Class: " + name);
    //scope of the class
    if (clazz.isPublic()) scope = "public";
    else if (clazz.isPrivate()) scope = "private";
    else if (clazz.isProtected()) scope = "protected";
    else scope = "default";
    System.out.println("Scope of the class: " + scope);
    //interface scope
    jclassInter = clazz.getInterfaces();

    for (JavaClass classInter : jclassInter){
    if (classInter.isPublic()) interfaceScope = "public";
    else if (classInter.isPrivate()) interfaceScope = "private";
    else if (classInter.isProtected()) interfaceScope = "protected";
    else interfaceScope = "default";
    }
    //package where the class belongs to
    jclassPackage = clazz.getPackageName();
    if (jclassPackage == null)
    jclassPackage = "default";
    //System.out.println("Package: " + jclassPackage);
    // name of the superclass
    sclass = clazz.getSuperclassName();
    //System.out.println("Superclass Name: " + sclass);
    // insert values into database

    if (interfaceScope == null){
    ps = connect.prepareStatement("INSERT INTO JavaFiles(className,classScope,package,superclassN ame,classFile) VALUES(?,?,?,?,?)");
    ps.setString(1, name);
    ps.setString(2, scope);
    ps.setString(3, jclassPackage);
    ps.setString(4, sclass);
    ps.setBinaryStream(5, is, (int)file.length());
    }
    else{
    ps=connect.prepareStatement("INSERT INTO JavaFiles VALUES (?,?,?,?,?,?)");
    ps.setString(1, name);
    ps.setString(2, scope);
    ps.setString(3, interfaceScope);
    ps.setString(4, jclassPackage);
    ps.setString(5, sclass);
    ps.setBinaryStream(6, is, (int)file.length());
    }
    int up = ps.executeUpdate();
    }catch(Exception e) {
    System.out.println(e.getMessage());
    }

    }

    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    And are you getting an exception?
    By the way, use printStackTrace for your exception handling. You're losing a load of useful information there otherwise.

    ETA: Oh, and use code tags otherwise your code loses formatting and becomes really hard to read.

  5. #5
    letscode is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    Where is the connection object initialized ?

    I can find only connection declaration in the pasted code. Initialise the connection object

    Example:

    connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial","root","root");

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Um.
    The Connection is passed into the constructor.

  7. #7
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    I have another class responsible for the jdbc connection...

  8. #8
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Is there a way to insert a file into a DB without PreparedStatement?
    Is the following insertion right?

    int up = statement.executeUpdate("INSERT INTO javaFiles(classFile) VALUES ('" + fis + "')");

    where fis is a FileInputStream variable.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    No.
    You need to use a prepared statment.

  10. #10
    Looserette is offline Member
    Join Date
    Feb 2011
    Posts
    12
    Rep Power
    0

    Default

    Hi,

    it looks somehow the same kind of error that I have, in this post:
    http://www.java-forums.org/database/...oracle-db.html

    I tried to give as much detail as possible in the other post ...
    A solution in one of both posts might solve many issues :)

    (and really sorry if the error is actually not the same)

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    It's not the same error.
    This is about someone not knowing how to use streams for clobs/blobs.

Similar Threads

  1. Regarding PreparedStatement
    By adeeb in forum JDBC
    Replies: 0
    Last Post: 06-09-2008, 09:07 PM
  2. Using PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 11:24 AM
  3. PreparedStatement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 03:56 PM
  4. Help me. PreparedStatement
    By Felissa in forum JDBC
    Replies: 2
    Last Post: 06-28-2007, 05:03 PM

Posting Permissions

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