📄 question.java
字号:
/**
* 问题咨询的Java Bean
* Question.java
* @author usr
*/
package building;
import java.lang.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
public class Question {
private int questionId = 0 ;
private int userId = 0 ;
private String title = null ;
private String contents = null ;
private String subtime = null ;
private int awCount = 0;
public Question(){
}
/*
* 问题Id set and get
*/
public void setQuestionId(int questionId){
this.questionId = questionId ;
}
public int getQuestionId(){
return (questionId) ;
}
/*
* 提问者用户ID set and get
*/
public void setUserId(int userId){
this.userId = userId ;
}
public int getUserId(){
return (userId) ;
}
/*
* 提问主题title set and get
*/
public void setTitle(String title){
this.title = title ;
}
public String getTitle(){
return (title) ;
}
/*
* 提问内容contents set and get
*/
public void setContents(String contents){
this.contents = contents ;
}
public String getContents(){
return (contents) ;
}
/*
* 提问时间subtime set and get
*/
public void setSubTime(String subtime){
this.subtime = subtime ;
}
public String getSubTime(){
return (subtime) ;
}
//回复的次数awCount的set及get方法
public void setAwCount(int awCount)
{
this.awCount = awCount;
}//End of setAwCount
public int getAwCount()
{
return(awCount);
}//End of getAwCount
/**
* 数据库操作方法
*/
/**
* 添加新问题
*/
public boolean addNewQuestion(DB db)
throws Exception
{
String strSql = null;
int iMax = 0;
ResultSet rs = null;
strSql = "select max(questionid) from Question";
rs = db.OpenSql(strSql);
if ( rs.next() )
{
//找到最大ID号
iMax = rs.getInt(1);
iMax++;
}//End of if
else
{
//没找到最大ID号
iMax = 1;
}//End of else
strSql = "insert into Question values("
+ iMax
+ "," + userId
+ ",'" + title
+ "','" + contents
+ "','" + subtime
+ "')";
if ( db.ExecSql(strSql) == 0 )
{
//添加失败
return(false);
}//End of if
else
{
//添加成功
return(true);
}//End of else
}//End of addNewQuestion
/**
* 查找指定用户ID的问题信息
*/
public static Vector getMyQuestions(DB db,int userid)
throws Exception
{
String strSql = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
Vector qtList = new Vector();
strSql = "select * from Question where userid=" + userid;
rs1 = db.OpenSql(strSql);
while(rs1.next())
{
Question qt = new Question();
qt.setQuestionId(rs1.getInt("questionid"));
qt.setUserId(rs1.getInt("userid"));
qt.setTitle(rs1.getString("title"));
qt.setContents(rs1.getString("contents"));
qt.setSubTime(rs1.getString("subtime"));
strSql = "select count(*) from Answer where questionid="
+ qt.getQuestionId();
rs2 = db.OpenSql(strSql);
if (rs2.next())
{
qt.setAwCount(rs2.getInt(1));
}//End of if
qtList.add(qt);
}//End of while
return(qtList);
}//End of getMyQuestions
/**
* 查找指定问题ID的信息
*/
public static Question getOneQuestion(DB db,int questionid)
throws SQLException
{
String strSql = null;
ResultSet rs = null;
Question qt = new Question();
strSql = "select * from Question where questionid=" + questionid;
rs = db.OpenSql(strSql);
if (rs.next())
{
qt.setQuestionId(rs.getInt("questionid"));
qt.setUserId(rs.getInt("userid"));
qt.setTitle(rs.getString("title"));
qt.setContents(rs.getString("contents"));
qt.setSubTime(rs.getString("subtime"));
return(qt);
}//End of if
else
{
return(null);
}//End of else
}//End of getOneQuestion
/**
* 查找所有的Question信息
*/
public static Vector getAllQuestions(DB db)
throws SQLException
{
String strSql = null;
ResultSet rs1, rs2;
Vector questionList = new Vector();
strSql = "select * from Question";
rs1 = db.OpenSql(strSql);
while (rs1.next())
{
Question qt = new Question();
qt.setQuestionId(rs1.getInt("questionid"));
qt.setUserId(rs1.getInt("userid"));
qt.setTitle(rs1.getString("title"));
qt.setContents(rs1.getString("contents"));
qt.setSubTime(rs1.getString("subtime"));
strSql = "select count(*) from Answer where questionid="
+ qt.getQuestionId();
rs2 = db.OpenSql(strSql);
if (rs2.next())
{
qt.setAwCount(rs2.getInt(1));
}//End of if
questionList.add(qt);
}//End of while
return(questionList);
}//End of getAllQuestions
/**
* 删除问题及其所有的信息
*/
public static boolean delQuestion(DB db,int questionId)
throws SQLException
{
String strSql = null;
strSql = "delete from Question where questionid=" + questionId;
if ( db.ExecSql(strSql) == 0 )
{
//删除失败
return(false);
}//End of if
else
{
//删除成功
return(true);
}//End of else
}//End of delQuestion
}//End of class Question
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -