-
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
-
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.
-
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());
}
}
}
-
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.
-
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");
-
Um.
The Connection is passed into the constructor.
-
I have another class responsible for the jdbc connection...
-
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.
-
No.
You need to use a prepared statment.
-
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)
-
It's not the same error.
This is about someone not knowing how to use streams for clobs/blobs.