我正在尝试建立一个小型音乐论坛,要求用户注册并登录以评论他们喜欢的歌曲,他们的信息将存储在mysql数据库中。我正在使用MVC模型来控制一切:注册,注销,登录。但现在我不知道如何让用户发表评论并在歌曲页面下方显示他们的所有评论(他们的评论也存储在数据库中)。这是我到目前为止尝试过的代码:
controller.java:
String action = request.getParameter("submit");
if (action.equals("Register")){
String Uname = request.getParameter("uname");
String Pword = request.getParameter("pass");
String Email = request.getParameter("mail");
boolean check = false;
try {
signin signin = new signin();
check = signin.check(Uname);
if (check)
request.setAttribute("error","This user has already been registered");
}
catch(Exception e){
System.out.println("Exception");
}
if (check){
RequestDispatcher dispatch = request.getRequestDispatcher("signin.jsp");
dispatch.forward(request, response);
}
else{
try{
signin signin = new signin();
signin.register(Uname, Email, Pword);
response.sendRedirect("registersuccess.jsp");
}
catch(Exception e)
{
System.out.println("Exception");
}
}
}
if (action.equals("Login")){
HttpSession session=request.getSession();
String username=request.getParameter("u1");
String password=request.getParameter("u2");
boolean check = false;
try {
signin a = new signin ();
check = a.checkuser(username, password);
}
catch(Exception e){System.out.println("Exception");}
if(check){
session.setAttribute("username",username);
response.sendRedirect("home.jsp");
}
else{
request.setAttribute("error","ERROR! Wrong Username or Password. Try again!");
RequestDispatcher dispatch=request.getRequestDispatcher("signinfail.jsp");
dispatch.forward(request, response);
}
}
if (action.equals("Logout")){
HttpSession session=request.getSession();
session.invalidate();
response.sendRedirect("home.jsp");
}
bean.java:
public void register(String uname, String pass, String email)
throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String url = "jdbc:mysql://localhost:3306/";
String dbName = "webauth";
String userName = "root";
String passWord = "";
Connection conn = DriverManager.getConnection(url + dbName, userName, passWord);
PreparedStatement statement;
statement = conn.prepareStatement("insert into data(username, email, password)" + " values (?,?,?)");
statement.setString(1, uname);
statement.setString(2, pass);
statement.setString(3, email);
statement.executeUpdate();
statement.clearParameters();
statement.close();
conn.close();
}
public boolean check(String username)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
boolean b = false;
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/webauth", "root", "");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT username FROM data");
while (rs.next()) {
if (rs.getString("username").equals(username)) {
b = true;
break;
} else {
b = false;
}
}
rs.close();
return b;
}
public boolean checkuser(String username, String password)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
boolean b = false;
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/webauth", "root", "");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT username,password FROM data");
while (rs.next()) {
if (rs.getString("username").equals(username) && rs.getString("password").equals(password)) {
b = true;
break;
} else {
b = false;
}
}
rs.close();
return b;
}
答案 0 :(得分:0)
如果歌曲具有唯一ID,请创建仅显示该歌曲评论的SQL查询,然后创建部分视图并使用该视图显示该歌曲的评论。
示例
从表中选择*,其中歌曲id =您的变量
然后,您可以创建一个显示该歌曲的所有评论的部分视图希望它指出你正确的方向