本人菜鸟,研究一个jsp+servlet+struts论坛的时候,发现可以和mysql正常连接,返回用户信息的时候,一部分项正常,但是对于某些项(webLog reg_time,last_time,reg_ip等),每次都返回NULL。我查了数据库,所有项都有对应的值,不是NULL。百思不得其解,特请教各位。。。
这是sql文件的一部分:
DROP TABLE IF EXISTS bbsuser
;
CREATE TABLE bbsuser
(id
int(11) NOT NULL auto_increment,username
varchar(20) NOT NULL default '',password
varchar(16) NOT NULL default '',nickname
varchar(20) NOT NULL default '',sex
varchar(3) default NULL,birthyear
varchar(4) default NULL,birthmonth
varchar(2) default NULL,birthday
varchar(2) default NULL,email
varchar(20) default NULL,weblog
varchar(30) default NULL,qq
varchar(11) default NULL,skype
varchar(16) default NULL,mobile
varchar(16) default NULL,signature
tinytext,reg_time
datetime default NULL,last_time
datetime default NULL,reg_ip
varchar(30) default NULL,last_ip
varchar(30) default NULL,info
tinytext,face
varchar(30) default NULL,online
int(1) default '0',islock
int(1) default '0',userscore
int(6) default '0',userpower
int(2) default '0',totalpost
int(8) default '0',usergarde
varchar(12) default 'user',
PRIMARY KEY (id
),
UNIQUE KEY name_index
(username
)
)ENGINE=MyISAM DEFAULT CHARSET=gbk;
这是显示出来的:
昵称: admin
发贴数量: 0
最后登陆: null
注册时间: null
性别: 男
生日: 1991年 02月 19日
E-mail: 33311113@163.com
WebLog: null
QQ: 276606757
Skype: 6949
Mobile: 15309290146
签名: Hallo
简介: null
这是与数据库连接的javabean:
package mybbs;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DBConn{
private Connection conn=null;
private Statement stmt=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public DBConn()
{
try{
//加载驱动程序
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
DataSource ds =(DataSource)ctx.lookup("jdbc/mybbs");
//创建连接
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.print(e.toString());
}
}
public synchronized Statement getStmt()throws Exception
{
//Statement stmt;
stmt=conn.createStatement();
return stmt;
}
//创建预处理会话对象
public synchronized PreparedStatement getPstmt(String sql)throws Exception
{
//PreparedStatement pstmt=null;
pstmt=conn.prepareStatement(sql);
return pstmt;
}
//执行查询操作
public synchronized ResultSet query(String sql)throws Exception
{
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
return rs;
}
catch(Exception e)
{
System.out.println(e.toString());
return null;
}
}
//执行更新操作
public synchronized int update(String sql)throws Exception
{
int nn;
try{
//Statement stmt;
stmt=conn.createStatement();
nn=stmt.executeUpdate(sql);
}
catch(Exception e)
{
System.out.println(e.toString());
nn=0;
}
return nn;
}
public void DBclose() throws SQLException {
if(rs!=null)
rs.close();
if(pstmt!=null)
pstmt.close();
if(stmt!=null)
stmt.close();
}
}
这是与返回用户信息的类:
package mybbs;
import java.sql.*;
public class DBUser
{
private String userid;
String username,password,nickname,sex,birthday,birthmonth,birthyear,e_mail,weblog,qq,skype,mobile,
signature,usergrade,reg_time,last_ip,reg_ip,last_time,info,face;
int userscore,userpower,totalpost;
public void DBUser(){
}
public void setUserid(String userid)
{
this.userid=userid;
}
public String getNickname(){
try{
DBConn conn=new DBConn();
String sql="SELECT * FROM bbsuser WHERE id="+userid;
ResultSet rs=conn.query(sql);
rs.next();
username=rs.getString("username");
password=rs.getString("password");
nickname=rs.getString("nickname");
sex=rs.getString("sex");
birthday=rs.getString("birthday");
birthmonth=rs.getString("birthmonth");
birthyear=rs.getString("birthyear");
e_mail=rs.getString("email");
qq=rs.getString("qq");
skype=rs.getString("skype");
mobile=rs.getString("mobile");
signature=rs.getString("signature");
usergrade=rs.getString("usergrade");
reg_time=rs.getString("reg_time");
last_ip=rs.getString("last_ip");
reg_ip=rs.getString("reg_ip");
last_time=rs.getString("last_time");
info=rs.getString("info");
weblog=rs.getString("weblog");
userscore=rs.getInt("userscore");
userpower=rs.getInt("userpower");
totalpost=rs.getInt("totalpost");
face=rs.getString("face");
rs.close();
}
catch(Exception e){}
return nickname;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
/*public String getNickname()
{
return nickname;
}*/
public String getEmail()
{
return e_mail;
}
public String getQq()
{
return qq;
}
public String getSkype()
{
return skype;
}
public String getRegtime()
{
return reg_time/*.substring(0,10)*/;
}
public String getLasttime()
{
return last_time/*.substring(0,10)*/;
}
public String getSignature()
{
return signature;
}
public String getUsergrade()
{
return usergrade;
}
public String getSex()
{
return sex;
}
public String getInfo()
{
return info;
}
public String getMobile()
{
return mobile;
}
public String getBirthday()
{
return birthday;
}
public String getBirthyear()
{
return birthyear;
}
public String getBirthmonth()
{
return birthmonth;
}
public int getUserscore()
{
return userscore;
}
public int getUserpower()
{
return userpower;
}
public int getTotalpost()
{
return totalpost;
}
public String getWeblog()
{
return weblog;
}
public String getFace()
{
return face;
}
}
如果加substring的话,直接就NULLPOINTEXCEPtion了,所以我注释掉了。
这是负责现实的jsp:
<%@ page contentType="text/html; charset=gbk" %>
<%@ page language="java" %>
<!--**************************************************************************************--> <% if(session.getAttribute("login")==null) response.sendRedirect("login.do"); String userid="0"; if(request.getParameter("userid")==null&&session.getAttribute("userid")!=null) userid=(String)session.getAttribute("userid"); else userid=request.getParameter("userid"); %> | ||
用户信息: | ||
昵称: |
用户信息: |
|
<!--**************************************************************************************-->
<BR>
<CENTER></CENTER><BR></TD></TR></TBODY></TABLE>