我正在尝试使用 java 代码插入图像,我能够插入图像,但我的表显示垃圾值这是我的代码,
用于创建表的代码。
try {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306 /Hotel",
"root", "root");
try {
Statement st = con.createStatement();
String table = "CREATE TABLE ItemMenu(Image MEDIUMBLOB,
Item_Codevarchar(20),Item_Name varchar(20),
Item_Category varchar(20),Sub_Category varchar(20),
Item_Rate varchar(20),Item_Kitchen varchar(20),
Item_Division varchar(20),Item_Description varchar(20))";
st.executeUpdate(table);
} catch (SQLException s) {
System.out.println("Table all ready exists!");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
将值插入表中的代码..
try {
File f = new File(path);
System.out.print(path);
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel", "root", "root123");
//这是从文本字段中获取值并存储在字符串中的代码。
try {
FileInputStream fis = new FileInputStream(f);
System.out.println("hello how r u");
String itemcode = itemcodefield.getText();
String itemname = itemnamefield.getText();
String categoryboxname = (String) categorybox.getSelectedItem();
String subcategoryboxname = (String) subcategorybox.getSelectedItem();
String rate = ratefield.getText();
String kitchenboxname = (String) kitchenbox.getSelectedItem();
String divisionboxname = (String) divisionbox.getSelectedItem();
String description = descriptionfield.getText();
String insert = "INSERT INTO ItemMenu(Image,Item_Code,
Item_Name,Item_Category,Sub_Category,Item_Rate,Item_Kitchen,
Item_Division,Item_Description)"
+ "VALUES (?,?,?,?,?,?,?,?,?)";
System.out.println("am fyn");
//这是 prepardstatemnt 的代码,它在 mysql 表中插入该变量。
PreparedStatement preparedStmt = con.prepareStatement(insert);
preparedStmt.setBinaryStream(1, (InputStream) f(int)
(f.length()
));
preparedStmt.setString(2, itemcode);
preparedStmt.setString(3, itemname);
preparedStmt.setString(4, categoryboxname);
preparedStmt.setString(5, subcategoryboxname);
preparedStmt.setString(6, rate);
preparedStmt.setString(7, kitchenboxname);
preparedStmt.setString(8, divisionboxname);
preparedStmt.setString(9, description);
preparedStmt.execute();
} catch (SQLException s) {
}
con.close();
} (Exception e1){
e1.printStackTrace();
}