當前位置:首頁 » 服務存儲 » jdbc存儲圖片大全
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

jdbc存儲圖片大全

發布時間: 2022-05-16 03:17:50

❶ 如何通過jdbc 讀取 存儲過程

給你個例子把,一看就明白了……
public List queryQYRBB(String date) throws ParseException {
CallableStatement proc = null;
List list = new ArrayList();
DbBean dbBean = new DbBean();

try {
proc = dbBean.conn.prepareCall("{call dbo.pr_YSumReport(?,?)}");

***********dbo.pr_YSumReport為存儲過程的名字************

proc.setString(1, date);
proc.setString(2, Util.getNextDay(date));
proc.execute();

ResultSet rs = proc.executeQuery();
list = dbBean.toListResultSet(rs);
} catch (sqlException e) {
e.printStackTrace();
} finally{
dbBean.close();
}
return list;
}

❷ JDBC往MYSQL存儲1G以上的BLOB欄位問題

1. 你資料庫欄位是什麼類型的?

MYSQL資料庫中的欄位大小如下:
BLOB 64KB
MEDIUMBLOB 16MB
LONGBLOB 4GB

2.我建議使用InputStreamBuffered 性能高

❸ java連接sql server2008中對圖片的存取和獲取問題(轉換成16進制數)

把圖片存儲到資料庫會很慢啊,一般都是把文件的名稱與路徑存儲到資料庫里,文件圖片存儲到文件夾里。

❹ java如何從資料庫中下載以二進制存儲的圖片

/**
* Created by IntelliJ IDEA.
* User: ljt
* Date: 2003-3-31
* Time: 18:51:38
* To change this template use Options | File Templates.
*/
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Clob;

public class TestOpenDoc {
public OracleResultSet ors = null; //**這里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //**PreparedStatement用
public Connection conn = null;
public Statement stmt = null;

public TestOpenDoc() {
}

public boolean getConnect() {
//這是我的資料庫所在
String serverName = "prosrv";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = DriverManager.getConnection(url, "appuser", "appuser");
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}

public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!test.getConnect()) {
System.out.println("資料庫連結錯誤");
return ;
}
try{

test.conn.setAutoCommit(false);
byte a[] = null; //**將測試文件test.doc讀入此位元組數組
java.io.FileInputStream fin = null;
java.io.FileOutputStream fout = null;

//Oracle提供的
try {
java.io.File f1 = new java.io.File("c:/test.doc");
java.io.File f2 = new java.io.File("d:/testout.doc"); //**從BLOB讀出的信息寫

//入該文 件,和源文件對比測試用
fin = new java.io.FileInputStream(f1);
fout = new java.io.FileOutputStream(f2);

int flength = (int) f1.length(); //**讀入文件的位元組長度
System.out.println("file length::" + flength);
a = new byte[flength];

int i = 0;
int itotal = 0;
//* 將文件讀入位元組數組
for (; itotal < flength; itotal = i + itotal) {
i = fin.read(a, itotal, flength - itotal);
}
fin.close();

System.out.println("read itotal::" + itotal);
//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
opst.setInt(2, flength);
opst.executeUpdate();
opst.clearParameters();
// /**插入其它數據後,定位BLOB欄位
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) opst.executeQuery();

if (ors.next()) {
oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB欄位
int j = blob.putBytes(1, a); //**將位元組數組寫入BLOB欄位
System.out.println("j:" + j);
test.conn.commit();
ors.close();
Clob clob;
clob = ors.getClob("");
String str;
str = clob.toString();
str = clob.getSubString(0L,(int)clob.length());
System.out.println(str);
}

System.out.println("insert into ok");

byte b[] = null; //**保存從BLOB讀出的位元組
opst.clearParameters();
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) test.conn.
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
ors = (OracleResultSet) opst.executeQuery();
if (ors.next()) {
oracle.sql.BLOB blob2 = ors.getBLOB(1);

System.out.println("blob2 length:" + blob2.length());
b = blob2.getBytes(1, flength); //**從BLOB取出位元組流數據
System.out.println("b length::" + b.length);
test.conn.commit();
}
ors.close();
// 將從BLOB讀出的位元組寫入文件
fout.write(b, 0, b.length);
fout.close();

System.out.println("write itotal::" + b.length);

}
catch (Exception e) {
System.out.println("errror :" + e.toString());
e.printStackTrace();

}
finally { //**關閉所有數據聯接
test.conn.commit();
}
}
catch(Exception e){
System.out.println(e);

}
}

}

❺ jdbc如何創建存儲過程

//創建存儲過程,記住關閉對象

importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

publicclassUseSQLDataBase3{
Connectioncon;
Statementstate;
ResultSetrs;
CallableStatementcs;//調用存儲過程使用的介面
Stringurl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
Stringuser="sa";
Stringpassword="";
publicvoidconnectSQL(){

try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(ClassNotFoundExceptione){
//TODO自動生成catch塊
e.printStackTrace();
}

try{
con=DriverManager.getConnection(url,user,password);
state=con.createStatement();


//創建存儲過程SQL語句
StringcreateProcere="createprocereSHOW_SUPPLIERS"+
"as"+
"selectSUPPLIERS.SUP_NAME,COFFEES.COF_NAME"+
"fromsuppliers,coffees"+
"wheresuppliers.sup_id=coffees.sup_id"+
"orderbysup_name";
//創建存儲過程
state.executeUpdate("USETEST");
state.executeUpdate(createProcere);

//調用存儲過程
cs=con.prepareCall("{callSHOW_SUPPLIERS}");//創建一個CallableStatement對象來調用資料庫存儲過程
//返回調用的結果集
rs=cs.executeQuery();

//輸出結果
System.out.println("SUPPLIERS.SUP_NAMECOFFEES.COF_NAME");
while(rs.next()){
Stringsup_name=rs.getString(1);
Stringcoffees_name=rs.getString(2);
System.out.println(sup_name+""+coffees_name);
}
con.close();
state.close();
}catch(SQLExceptione){
//TODO自動生成catch塊
e.printStackTrace();
}

}

❻ 如何在Java程序中選擇添加圖片,再存到資料庫中

存儲圖片:

//載入驅動程序類
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
//建立資料庫聯機,其中denglu為資料庫名,sa為連接資料庫的帳號及密碼。
Statement stmt=con.createStatement(); //建立Statement對象

FileInputStream str=new FileInputStream(filename); //圖片文件路徑
String sql="insert into picturenews(id,image) values(?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,id); //ID號
pstmt.setBinaryStream(2,str,str.available()); //圖片數據
pstmt.execute();
//將數據存入資料庫
out.println("Success,You Have Insert an Image Successfully");

圖片讀取:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
ResultSet rs=null;
String sql = "select image from picturenews WHERE id="+id;

rs=stmt.executeQuery(sql);
if(rs.next()) {
//圖片輸出的輸出流
InputStream in = rs.getBinaryStream("image");
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
//將緩沖區的輸入輸出到頁面
in.read(b);
}
}

/**
* 獲得數據後可以按照自己的方法進行處理或者顯示
*/
JLabel label=new JLabel(new ImageIcon(b)); //用JLabel進行顯示

.....

❼ 資料庫里存儲的圖片欄位定義類型為blob,java里對應的類型為byte[],怎麼將圖片顯示到jsp頁面

我把我以前收集的給你貼出來,希望能對你有所幫助 jsp編程從資料庫中取出圖片 1、讀取圖片數據testimageout.jsp文件 <%@ page contentType="text/html;charset=gb2312"% <%@ page import="java.sql.*" % <%@ page import="java.util.*" % <%@ page import="java.text.*" % <%@ page import="java.io.*" %<html<body<%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa"); Statement stmt=con.createStatement(); ResultSet rs=null; int id=Integer.parseInt(request.getParameter("id")); String sql = "select image from picturenews where id='"+id+"'"; rs=stmt.executeQuery(sql); while(rs.next()){ServletOutputStream sout = response.getOutputStream(); InputStream in = rs.getBinaryStream(1); byte b[] = new byte[0x7a120]; for(int i = in.read(b);i!=-1){sout.write(b); in.read(b);}sout.flush(); sout.close();}%<body</html2、取出所要顯示的圖片showimage.jsp文件 <%@ page contentType="text/html;charset=bg2312"% <%@ page import="java.sql.*" %<html<head<title顯示資料庫圖片測試頁</title</head<body<%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa"); Statement stmt=con.createStatement(); String sql=new String(); sql = "select id from picturenews"; id=<%=rs.getInt("id")%'</td</tr</table</body</html

❽ 圖片如何存放在oracle資料庫

1、使用blob將圖片保存為二進制格式,(可以用瀏覽器來轉換)隨後用base64編碼來保存圖片,再將base64編碼保存進資料庫的clob類型欄位上。

❾ java向SQLite保存圖片

sqlite作為一個在手機等小設備上使用的微型資料庫,只有有限的集中數據類型,text, numeric,blob, integer primary key。所以你最好選用blob欄位類型。
jdbc直接使用https://bitbucket.org/xerial/sqlite-jdbc。由於要使用流,你也應該使用sqlite提供的實現方式。