Image Insertion into MySQL database Using java JDBC
For explanation watch video:
Code::
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class Test {
private static final String query = "INSERT INTO IMG_TAB(CNAME,IMAGE) VALUES(?,?)";
public static void main(String[] args)throws Exception{
Scanner scn = new Scanner(System.in);
System.out.println("Enter Customer name::");
String cname = scn.next();
System.out.println("Enter the image location: ");
String loc = scn.next().replace("?", "");
InputStream is = new FileInputStream(loc);
Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root");
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, cname);
ps.setBinaryStream(2, is);
int count = ps.executeUpdate();
if(count!=0) {
System.out.println("Info filled");
}else {
System.out.println("Info not filled");
}
con.close();
}
}
Comments
Post a Comment