i want to add this image to database how can i do
please help me
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
i want to add this image to database how can i do
please help me
You know how you connect to a server?
--- Update ---
And who database you will used?
--- Update ---
I think when you see this code you have a ide how you can do the :-)
import java.sql.*;
public class Databasekommunikation
{
public static void main(String[] arg) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver indlęst");
Connection forb = DriverManager.getConnection(
"jdbcracle:thin:@ora.javabog.dk:1521:student","jacob"," jacob");
System.out.println("Forbindelse oprettet");
Statement stmt = forb.createStatement();
try { stmt.executeUpdate("drop table KUNDER"); } catch (Exception e) {}
stmt.executeUpdate("create table KUNDER (NAVN varchar(32), KREDIT number)");
System.out.println("Tabel oprettet");
stmt.executeUpdate("insert into KUNDER values('Jacob', -1799)");
stmt.executeUpdate("insert into KUNDER(NAVN,KREDIT) values('Brian', 0)");
String navn = "Hans";
double kredit = 500;
stmt.executeUpdate(
"insert into KUNDER(NAVN,KREDIT) values('"+navn+"', "+kredit+")");
ResultSet rs = stmt.executeQuery("select NAME, CREDIT from KUNDER");
while (rs.next())
{
name = rs.getString("NAME");
kredit = rs.getDouble("KREDIT");
System.out.println(navn+" "+kredit);
}
}
}
Driver indlęst
Forbindelse oprettet
Tabel oprettet
Jacob -1799.0
Brian 0.0
Hans 500.0
Example how you can send things to you database. Here was the database MySql :-)
Hope you can now. Or write here... You shall not see on they words you not understand just look on the programming.
Last edited by magnus110498; March 18th, 2014 at 10:39 AM.
Here is a basic example for adding image to database using java.
public static void main(String[] args) throws Exception {
// Select file
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILE S_ONLY);
fileChooser.showOpenDialog(null);
File selectedFile = fileChooser.getSelectedFile();
// Insert image
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "insert into MyPictures(photo) values (?)";
conn.setAutoCommit(false);
FileInputStream fis = new FileInputStream(selectedFile);
PreparedStatement ps = conn.prepareStatement(INSERT_PICTURE);
ps.setBinaryStream(1, fis, (int) selectedFile.length());
ps.executeUpdate();
conn.commit();
ps.close();
fis.close();
conn.close();
// Show image
BufferedImage bufferedImage = ImageIO.read(selectedFile);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
JLabel label = new JLabel();
label.setIcon(imageIcon);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add(label);
frame.pack();
frame.setVisible(true);
}