Results 1 to 11 of 11
Thread: preparedStatement
- 03-15-2011, 11:07 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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.classLast edited by axenos; 03-15-2011 at 11:11 PM.
- 03-16-2011, 08:51 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,470
- Rep Power
- 16
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.
- 03-16-2011, 09:44 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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());
}
}
}
- 03-16-2011, 10:10 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,470
- Rep Power
- 16
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.
- 03-16-2011, 10:47 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
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");
- 03-16-2011, 10:57 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,470
- Rep Power
- 16
Um.
The Connection is passed into the constructor.
- 03-16-2011, 11:13 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
I have another class responsible for the jdbc connection...
- 03-18-2011, 08:30 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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.
- 03-21-2011, 09:31 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,470
- Rep Power
- 16
No.
You need to use a prepared statment.
- 03-31-2011, 10:54 AM #10
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
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)
- 03-31-2011, 11:00 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,470
- Rep Power
- 16
Similar Threads
-
Regarding PreparedStatement
By adeeb in forum JDBCReplies: 0Last Post: 06-09-2008, 09:07 PM -
Using PreparedStatement
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:24 AM -
PreparedStatement
By Java Tip in forum Java TipReplies: 0Last Post: 12-05-2007, 03:56 PM -
Help me. PreparedStatement
By Felissa in forum JDBCReplies: 2Last Post: 06-28-2007, 05:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks