这是连接数据库
package upload;
import java.sql.*;
import java.io.*;
public class connect{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/upload";
Connection con=null; //声明一个共享的连接对象
Statement stat=null;
ResultSet rs=null;
int rowCount=0; //总的记录数
public connect(){
try{
Class.forName(driver);
con=DriverManager.getConnection(url,"root","java");
System.out.print("连接成功");
}catch(Exception e){e.printStackTrace();
}
}
//取得数据库连接
public Connection getConn(){
return con;
}
//执行sql语句
public ResultSet myQuery(String sql) throws SQLException{
stat=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//结果集的游标可上下移动,可更新数据库中的表
rs=stat.executeQuery(sql);//返回结果集.
return rs;
}
//关闭数据库连接
public void myClose() throws SQLException{
con.close();
}
}
这是插入数据
package upload;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.text.*;
import java.sql.*;
import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.*;
public class upload extends HttpServlet{
private static final String CONTENT_TYPE="text/html; charset=GBK";
Connection conn;
int maxPostSize=3*5*1024*1024;
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException ,IOException{
String name=request.getParameter("name");
response.setContentType(CONTENT_TYPE);
String dirs=request.getRealPath("/");
String saveDirectory=dirs+"photo";
PrintWriter out=response.getWriter();
connect connectdata=new connect();
conn=connectdata.getConn();
MultipartRequest multi=new MultipartRequest(request,saveDirectory,maxPostSize,"GBK");
Enumeration files=multi.getFileNames(); //处理文件的上传
while(files.hasMoreElements()){
String names=(String)files.nextElement();
File f=multi.getFile(names);
if(f!=null){
String filename=multi.getFilesystemName(names);
String lastFilename=saveDirectory+"\\"+filename;
String sql="insert into java3(name,filename) values('"+name+"','"+filename+"')";
try{
Statement stmt=conn.createStatement();
stmt.execute(sql);
out.print("<script> alert('成功添加新成员!'); this.location.href='show.jsp'</script>");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
这是上传图片并在当前页显示
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="/show/upload">
<table><tr><td>
<p>图片名称:
<label>
<input type="text" name="name" />
</label>
</p>
<p>
<input type="file" name="file"/>
</p></td></tr>
<tr><td><input type="submit" name="submit" value="确定"/></td></tr>
</table>
<%
Statement stmt;
ResultSet rs=null;
Connection conn;
try{
String sql="select * from zhang order by id desc";
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/upload","root","java");
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.executeQuery(sql);
while(rs.next()){
String file=rs.getString("filename");
String name=rs.getString("name");
%>
<table border="0" cellpadding="0" cellspacing="0" height="150" width="150">
<tr><td><%=name%></td></tr>
<tr>
<td width="196" rowspan="5">
<div align="center"><A
href="photo/<%=file%>" target=_blank><img src="photo/<%=file%>" width="100" height="100" hspace="0" vspace="0" border="0" /></A></div></td>
</tr>
</table>
<%}
}catch(Exception e){out.print(e);}
%>
</form>
</body>
</html>
结果不出现显示 而且数据存不进去数据库中 但在临时目录photo中存有图片
出现 java.lang.NullPointerException 错误
这是我建的表
create database upload;
use upload;
create table zhang(id int(30) not null auto_increment,name varchar(20) not null,filename varchar(50) not null,primary key (id));
我都改了半天了 还是改不成功
到底是哪错了 请各位 师哥师姐们帮我看看
谢谢!