谁能把下面的jsp给改成java

oCaoMao12 2012-09-02 03:44:44
<%@ 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>
...全文
144 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackson_fighting 2012-09-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

楼主是刚开始学 MVC?想把逻辑处理抽离到Servlet中去?

剥离Java代码到不算难,把Servlet和JSP之间的forward方式以及如何借助request的attribute来传递参数弄通,整个就都比较简单了。
[/Quote]
估计楼主想做一个 JDBCConnectionUtil
MiceRice 2012-09-02
  • 打赏
  • 举报
回复
楼主是刚开始学 MVC?想把逻辑处理抽离到Servlet中去?

剥离Java代码到不算难,把Servlet和JSP之间的forward方式以及如何借助request的attribute来传递参数弄通,整个就都比较简单了。
代码之abc 2012-09-02
  • 打赏
  • 举报
回复
改啥啊 ,直接把<% %>之间的内容提取出来就行了,<% %>之外的都删了

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧