向MySql數據庫插入與讀取圖片文件

1、插入圖片
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestAdd {
/**java

  • 測試向mysql添加大字段

*/
public void testAdd() {mysql

// 1.create sql ;
String sql = "insert into userinfo ( username , password , image) values (?,?,?)";android

// 2.get connection
Connection conn = null;sql

PreparedStatement psmt = null;數據庫

InputStream is = null; 測試

try {code

conn = JdbcHelper.getConnection(); 圖片

// 3.prepare sql
psmt = conn.prepareStatement(sql);資源

// 4.set paramsrem

psmt.setString(1, "javaee");
psmt.setString(2, "123456");

// (1)get the stream
is = new FileInputStream("F:/androidgo/院內信息資源整合系統創建.bmp");
psmt.setBinaryStream(3, is, is.available());

// 5.update db
psmt.executeUpdate();

System.out.println("ok!");

} catch (Exception e) {
e.printStackTrace();
} finally {
// 6.close db
try {
if (psmt != null)
psmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}
}

public static void main(String[] args) {

TestAdd test = new TestAdd();

test.testAdd();
}
}2、獲取圖片import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestAdd {
/**

  • 測試向mysql添加大字段

*/
public void testAdd() {

// 1.create sql ;
String sql = "insert into userinfo ( username , password , image) values (?,?,?)";

// 2.get connection
Connection conn = null;

PreparedStatement psmt = null;

InputStream is = null;

try {

conn = JdbcHelper.getConnection();

// 3.prepare sql
psmt = conn.prepareStatement(sql);

// 4.set params

psmt.setString(1, "javaee");
psmt.setString(2, "123456");

// (1)get the stream
is = new FileInputStream("F:/androidgo/院內信息資源整合系統創建.bmp");
psmt.setBinaryStream(3, is, is.available());

// 5.update db
psmt.executeUpdate();

System.out.println("ok!");

} catch (Exception e) {
e.printStackTrace();
} finally {
// 6.close db
try {
if (psmt != null)
psmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}
}

public static void main(String[] args) {

TestAdd test = new TestAdd();

test.testAdd();
}
}
3、助手類
import java.sql.Connection;
import java.sql.DriverManager;
/**

  • @author Administrator
  • =====================返回數據庫的鏈接信息==========================

*/
public class JdbcHelper {
private static Connection conn;
private JdbcHelper() {
}
// get the connection to db
public static synchronized Connection getConnection() throws Exception {
if (conn == null)
initConnection();
else if (conn.isClosed())
initConnection();
return conn;
}
//
private static void initConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.101:3306/android",
"root", "123456");
}
}
4、數據庫文件CREATE TABLE userinfo ( id int(10) unsigned NOT NULL auto_increment, username varchar(45) default NULL, password varchar(45) default NULL, image longblob, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

相關文章
相關標籤/搜索