import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.PreparedStatement;
public class Input {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
System.out.println("数据库链接成功");
String sql="insert into user value (?,?,?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "张三");
ps.setString(3, "123456");
File file=new File("D:/photo/1.jpg");//1
FileInputStream fis=new FileInputStream(file);//2
ps.setBinaryStream(4, fis, (int)file.length());
ps.executeUpdate();
System.out.println("插入数据成功");
if (conn != null) {
ps.close();
fis.close();
conn.close();
System.out.println("链接关闭成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
数据库图片导入
最新推荐文章于 2021-07-01 20:49:14 发布