Can anyone help me to improve my code so i can add more than image to my sql database. The code i use is below:

Connection connection = null;
String connectionURL = “jdbc:mysql://localhost:3306/mydb”;
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
Class.forName(“com.mysql.jdbc.Driver”);//.newInstance();
connection = DriverManager.getConnection(connectionURL, “root”, “magnet”);
File image = new File(“c:/Me.jpg”);
psmnt = connection.prepareStatement(“insert into images(image) “+ “values(?)”);
fis = new FileInputStream(image); 
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
int flag= psmnt.executeUpdate();
if(flag>0) 
{
System.out.println(“Uploaded successfully !”);
}
else 
{
System.out.println(“unsucessfull to upload image.”);
}
}
catch (Exception ex) 
{
System.out.println(“Found some error : “+ex);
}
finally 
{ 
connection.close();
psmnt.close();
}
}

Recommended Answers

All 6 Replies

I hava tried to add this to the above code but i keep getting errors.
Please help!!

String[] myImageNames = new String[] {"C:\\Pic1.jpg", "C:\\Pic2.jpg", "C:\\Pic3.jpg"}

for (int i = 0; i < myImageNames.length; i++)
{
    File myFile = new File(myImageNames[i]);


}

so you have code that inserts one image into your db?
how about calling that code repeatedly?

i did try that but it makes errors also i dont want a lonnnnnng code. i want to be able to upload the images into the database when i run the code the once.

what do you mean by ' lonnnnnnnnng code? the code would be the full two lines longer, one of those lines being:
}

can't really be that much a problem, now can it?
can you specify 'makes errors'?
do you get compile time or runtime errors? which errors?

i guess i dont understand what you are saying then if its 2 lines of code, care to elaborate?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.