Step 1: Create a database
Create Table test_img (
ID number (4 ),
Name varchar (20 ),
IMG long raw );
Step 2:
<HTML> <Body>
Add image <br>
<Form method = "Post" Action = "insertnews. jsp">
Image ID: <input name = "ID" size = "10">
<Br>
Select image: <input type = "file" name = "image">
<Br>
<Input type = "Submit" value = "Upload" name = "Submit" size = "25">
<Input type = "reset" value = "clear" name = "clear" size = "25">
<Br>
</Form>
</Body>
Step 3: insert the database (insertimg. jsp)
<% @ Page Language = "Java" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "Java. Text. *" %>
<% @ Page import = "Java. Io. *" %>
<%
Class. forname ("oracle. JDBC. Driver. oracledriver ");
String url = "JDBC: oracle: thin: @ Lubin: 1521: b2bdb ";
Connection con = drivermanager. getconnection (URL, "Demo", "Demo ");
// Insert a database
String SQL = "insert into test_img values (?,?,?) ";
// Obtain the upload value ID
String id = request. getparameter ("ID ");
// Obtain the image path
String KK = request. getparameter ("image ");
// Convert to file format
File filename = new file (kk );
// Read the file length and convert it to the long type
Long L1 = filename. Length ();
Int L2 = (INT) L1;
// Value assignment in stream format
Fileinputstream FCM = new fileinputstream (filename );
Preparedstatement PS = con. preparestatement (SQL );
PS. setstring (1, ID );
PS. setstring (2, filename. getname ());
PS. setbinarystream (3, FS, L2 );
Ps.exe cuteupdate ();
PS. Close ();
FCM. Close ();