<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.util.*"%> 
<%@ page import="java.text.*"%> 
<%@ page import="java.io.*"%>
<%@ page import="java.nio.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
    <body>
     <%
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //加载JDBC驱动程序   
        String  url="jdbc:mysql://localhost/bin_db"; 
       //bin_db为你的数据库的名称   
        String  user="root";    
        String  password="root";          
      
        String filename=request.getParameter("image");
         File file = new File(filename); //获取表单传过来的图片的url           
        try {
//打开文件
FileInputStream fin = new FileInputStream(file);
//建一个缓冲保存数据
ByteBuffer nbf = ByteBuffer.allocate((int) file.length());
byte[] array = new byte[1024];
int offset = 0, length = 0;
//读存数据
while ((length = fin.read(array)) > 0) {
if (length != 1024)
nbf.put(array, 0, length);
else
nbf.put(array);
offset += length;
}
  //新建一个数组保存要写的内容
byte[] content = nbf.array();
//创建数据库连接
Connection  conn=  DriverManager.getConnection(url,user,password);
//保存数据
Statement stmt =conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sqlstr = "select * from bindata where filename='01'";
ResultSet rs = stmt.executeQuery(sqlstr);
if (rs.next()) 
{
rs.updateBytes(2, content);
rs.updateRow();
} else {
rs.moveToInsertRow();
rs.updateString(1, "01");
rs.updateBytes(2, content);
rs.insertRow();
}
rs.close();
// 关闭文件
fin.close();
out.println("恭喜,已经将新的记录成功地添加到数据库中!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
   %> 
    </body>
</html>

解决方案 »

  1.   

    改啥啊 ,直接把<%  %>之间的内容提取出来就行了,<%  %>之外的都删了
      

  2.   

    楼主是刚开始学 MVC?想把逻辑处理抽离到Servlet中去?剥离Java代码到不算难,把Servlet和JSP之间的forward方式以及如何借助request的attribute来传递参数弄通,整个就都比较简单了。
      

  3.   

    估计楼主想做一个 JDBCConnectionUtil