📄 createimageblob1.java~2~
字号:
package cmp2image;import java.sql.*;import java.io.*;public class CreateImageBlob1 { public static void main(String[] args) throws Exception { String dbUrl = "jdbc:mysql://127.0.0.1/mydb1"; String user = ""; String password = ""; // Load the driver Class.forName("com.mysql.jdbc.Driver"); //connecting the databse Connection con = DriverManager.getConnection( dbUrl, user, password); String createTable = "CREATE TABLE Flower" + " (Name VARCHAR(250) NOT NULL PRIMARY KEY, "+ " IMGBYTES BLOB )"; Statement s = con.createStatement(); s.executeUpdate(createTable); String insertSQL = "Insert INTO Flower(Name,IMGBYTES) "+ " VALUES(?,?)"; PreparedStatement pstmt = con.prepareStatement(insertSQL); System.out.println("Connection OK"); File file = new File("D:\\WebDesignCodes\\MyPlants\\images\\FlowerAfricanOrchid.jpg"); FileInputStream fis = new FileInputStream(file); pstmt.setString(1, "AfricanOrchid"); pstmt.setBinaryStream(2, fis, (int) file.length()); pstmt.executeUpdate(); insertSQL = "Insert INTO Flower(Name,IMGBYTES) "+ " VALUES(?,?)"; pstmt = con.prepareStatement(insertSQL); file = new File("D:\\WebDesignCodes\\MyPlants\\images\\FlowerBabyBreath.jpg"); fis = new FileInputStream(file); pstmt.setString(1, "FlowerBabyBreath"); pstmt.setBinaryStream(2, fis, (int) file.length()); insertSQL = "Insert INTO Flower(Name,IMGBYTES) "+ " VALUES(?,?)"; pstmt = con.prepareStatement(insertSQL); file = new File("D:\\WebDesignCodes\\MyPlants\\images\\FlowerBlackEyedSusan.jpg"); fis = new FileInputStream(file); pstmt.setString(1, "FlowerBlackEyedSusan"); pstmt.setBinaryStream(2, fis, (int) file.length()); pstmt.close(); s.close(); con.close(); fis.close(); System.out.println("Image Insert Successful"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -