📄 guestbook.java
字号:
package ch08;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
/**
* @author <a href="wei.cheng@dudu-inc.com">Cheng Wei</a>
* 留言本的管理类
*/
public class Guestbook {
//数据库连接
private Connection conn;
//错误
public String error;
//留言本标题
private String title;
//每页显示留言条数
private int pageshownumber;
//留言总数
private int mes_total;
//分页总数
private int page_total;
//当前页面
private int currentpage;
//管理员用户
private String adm_user;
//管理员密码
private String adm_psw;
/**
* 留言本的构造类 系统初始化后设置整个系统中需要的各个参数变量
*/
public Guestbook() {
conn = null;
error = null;
title = null;
pageshownumber = 15;
mes_total = 0;
page_total = 0;
currentpage = 1;
adm_user = null;
adm_psw = null;
}
/**
* 系统初始化,完成数据库连接
*
* @param driver 驱动
* @param mysqlUrl 路径
* @param user 用户
* @param password 密码
* @return
*/
public boolean init(String driver, String mysqlUrl, String user,
String password) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(mysqlUrl, user, password);
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 系统初始化,完成数据库连接
*
* @param driver
* @param mysqlUrl
* @param user
* @param password
* @return
*/
public boolean init2(String driver, String mysqlUrl, String user,
String password) {
try {
Class.forName(driver);
String dburl = mysqlUrl + "?user=" + user + "&password=" + password
+ "&useUnicode=true&characterEncoding=GBK";
conn = DriverManager.getConnection(dburl);
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 字符串编码转换成GBK
*
* @param str 需要转码的字符串
* @return 转码后的字符串
*/
public String getStr(String str) {
String s2;
String s1 = str;
try {
byte abyte0[] = s1.getBytes("GBK");
s2 = new String(abyte0);
return s2;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* 处理HTML中的特殊字符
* @param str
* @return
*/
public String HtmlSpecialChars(String str) {
if (str == null || str.equals(""))
return str;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == '\n')
sb = sb.append("<br>");
else if (str.charAt(i) == ' ')
sb = sb.append(" ");
else if (str.charAt(i) == '<')
sb = sb.append("<");
else if (str.charAt(i) == '>')
sb = sb.append(">");
else
sb = sb.append(str.substring(i, i + 1));
String newstr = sb.toString();
return newstr;
}
/**
* 发表文章
* @param as
* @return
*/
public boolean post(String info[]) {
String name;
String qq;
String email;
String homepage;
String headima;
String word;
long l;
name = info[0];
qq = info[1];
email = info[2];
homepage = info[3];
headima = info[4];
word = info[5];
if (qq == "" || qq == null || qq.equals(""))
qq = "no";
if (email == "" || email == null || email.equals(""))
email = "no";
if (homepage == "" || homepage == null || homepage.equals(""))
homepage = "no";
Date date = new Date();
l = date.getTime();
PreparedStatement preparedstatement;
try {
preparedstatement = conn
.prepareStatement("insert into `guestbook` values('',?,?,?,?,?,?,?)");
preparedstatement.setString(1, name);
preparedstatement.setString(2, qq);
preparedstatement.setString(3, email);
preparedstatement.setString(4, homepage);
preparedstatement.setString(5, headima);
preparedstatement.setLong(6, l);
preparedstatement.setString(7, word);
int i = preparedstatement.executeUpdate();
if (i != 1)
return false;
if (preparedstatement != null)
preparedstatement.close();
if (conn != null)
conn.close();
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 获取系统的配置信息,包括标题,每页显示留言条数。
*
*/
public void getconfig() {
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select `title`,`pageshownumber` from `guestbook_admin` where `id`=4");
ResultSet resultset = preparedstatement.executeQuery();
resultset.next();
title = resultset.getString(1);
pageshownumber = resultset.getInt(2);
resultset.close();
preparedstatement.close();
} catch (Exception exception) {
error = exception.toString();
}
}
/**
* 获取标题
*
* @return
*/
public String gettitle() {
return title;
}
/**
* 获取每页显示条数
*
* @return
*/
public int getpageshownumber() {
return pageshownumber;
}
/**
* 获取留言总数
*
* @return
*/
public int getmes_total() {
return mes_total;
}
/**
* 设置管理员用户
*
* @param s
*/
public void setadm_user(String s) {
adm_user = s;
}
/**
* 设置管理员密码
*
* @param s
*/
public void setadm_psw(String s) {
adm_psw = s;
}
/**
* 获取分页总数
*
* @return
*/
public int getpage_total() {
return page_total;
}
/**
* 获取当前页面
*
* @param i
*/
public void currentpage(int i) {
currentpage = i;
}
/**
* 获取留言总数
*
*/
public void mes_total() {
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select count(*) as `total` from `guestbook`");
ResultSet resultset = preparedstatement.executeQuery();
resultset.next();
mes_total = resultset.getInt(1);
resultset.close();
preparedstatement.close();
} catch (Exception exception) {
error = exception.toString();
exception.printStackTrace();
}
}
/**
* 计算全部留言页面
*/
public void page_total() {
double d = (double) mes_total / (double) pageshownumber;
double d1 = mes_total / pageshownumber;
if (d > d1)
page_total = (int) d1 + 1;
else
page_total = (int) d1;
}
/**
* 获取留言总数
*
* @return resultset结果集
*/
public ResultSet query() {
ResultSet resultset;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select * from `guestbook` order by `time` desc limit ?,?");
int i = (currentpage - 1) * pageshownumber;
preparedstatement.setInt(1, i);
preparedstatement.setInt(2, pageshownumber);
resultset = preparedstatement.executeQuery();
return resultset;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return null;
}
}
/**
* 管理员认证
*
* @return
*/
public boolean admCheck() {
int i = 0;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select `id` from `guestbook_admin` where `user`='"
+ adm_user + "' and `psw`='" + adm_psw + "'");
ResultSet resultset;
for (resultset = preparedstatement.executeQuery(); resultset.next();)
i++;
resultset.close();
preparedstatement.close();
if (i != 1)
return false;
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 管理员登陆
*
* @param user
* @param password
* @return
*/
public boolean admLogin(String user, String password) {
int i = 0;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select `id` from `guestbook_admin` where `user`='"
+ user + "' and `psw`=PASSWORD('" + password + "')");
ResultSet resultset;
for (resultset = preparedstatement.executeQuery(); resultset.next();)
i++;
resultset.close();
preparedstatement.close();
if (i != 1)
return false;
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 获取用户的密码
*
* @param user
* @param password
* @return
*/
public String getPassword(String user, String password) {
String s2 = null;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("select `id`,`psw` from `guestbook_admin` where `user`='"
+ user + "' and `psw`=PASSWORD('" + password + "')");
ResultSet resultset = preparedstatement.executeQuery();
resultset.next();
s2 = resultset.getString(2);
resultset.close();
return s2;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return null;
}
}
/**
* 删除留言
*
* @param i
* @return
*/
public boolean delete(int i) {
int j;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("delete from `guestbook` where `id`=?");
preparedstatement.setInt(1, i);
j = preparedstatement.executeUpdate();
preparedstatement.close();
if (j != 1)
return false;
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 更新系统信息
*
* @param title 留言簿标题
* @param num 留言本每页显示留言条数
* @return
*/
public boolean system(String title, int num) {
int j;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("update `guestbook_admin` set `title`=?,`pageshownumber`=?");
preparedstatement.setString(1, title);
preparedstatement.setInt(2, num);
j = preparedstatement.executeUpdate();
preparedstatement.close();
if (j != 1)
return false;
return true;
} catch (Exception exception) {
exception.printStackTrace();
error = exception.toString();
return false;
}
}
/**
* 更改用户的密码
*
* @param user
* @param oldpass
* @param newpass
* @return
*/
public boolean changepsw(String user, String oldpass, String newpass) {
int i;
try {
PreparedStatement preparedstatement = conn
.prepareStatement("update `guestbook_admin` set `psw`=PASSWORD('"
+ newpass
+ "') where `user`='"
+ user
+ "' and `psw`=PASSWORD('" + oldpass + "')");
i = preparedstatement.executeUpdate();
preparedstatement.close();
error = String.valueOf(i);
if (i != 1)
return false;
return true;
} catch (Exception ex) {
ex.printStackTrace();
error = ex.toString();
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -