 |
| JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 18th, 2005, 01:46 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
MySql, JSP, javaBeans! help!
Hello everybody..
i'm trying to develop a registeration code using JSP, MySql and Java beans..
but it doesn't work!!
well.. the idea is the following:
first i'm using a studentBean to set and get the relevant information of the student!
-------------------------------
package myBeans;
public class studentBean {
int reg_num;
String name;
String surname;
String course;
String username;
public void setusername(String username) {
this.username = username;
}
public void setname(String name) {
this.name = name;
}
public void setsurname(String surname) {
this.surname = surname;
}
public void setcourse(String course) {
this.course = course;
}
public void setreg_num(int reg_num) {
this.reg_num = reg_num;
}
public int getreg_num() {
return reg_num;
}
public String getusername() {
return username;
}
public String getFullName() {
return name + " " + surname;
}
public String getcourse() {
return course;
}
}
-------------------------------------------
then i'm using register.jsp
<html>
<head><title>Welcome to the Registration Page</title></head>
<body>
<h3 align="center">Registration Page</h3>
<form action="checkregister.jsp" method="POST">
<table border="0">
<tr><td>Username:<input type="text" name="username"/></td></tr>
<tr><td>Name :<input type="text" name="name"/></td></tr>
<tr><td>Surname :<input type="text" name="surname"/></td></tr>
<tr><td> Course:
<select name = "course">
<option value = "BSc CS">Computer Science</option>
<option value = "BSc CoE">Computer Engineering</option>
<option value = "BSc GD">Graphic Design</option>
<option value = "BSc CE">Civil Engineering</option>
<option value = "BSc AE">Automotive Engineering</option>
</select>
</tr> </td>
<tr><td><input type="submit" value="Sign Up"/></td></tr>
</table>
</form>
</body>
</html>
--------------------------------------------------
here's the Java Bean:
authBean2.java
package myBeans;
import java.sql.*;
import java.sql.ResultSet;
import myBeans.studentBean;
import java.util.Vector;
import javax.servlet.jsp.JspWriter;
public class authBean2 {
Connection cn = null;
public authBean2() {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/ProjectDB?user=root&password=acsc404");
}
catch (Exception e) { System.out.println(e.getMessage()); }
}
public studentBean addStudent(String username, String name,
String surname, String course, JspWriter out) {
ResultSet rs = null;
Statement st = null;
String sqlStr = null;
try {
st = cn.createStatement();
sqlStr = " SELECT reg_num FROM student WHERE username = ' " + username + "' " ;
rs = st.executeQuery(sqlStr);
if (rs.next()) {
return null;
}
else {
st = cn.createStatement();
sqlStr = " INSERT INTO student " +
"(username, name, surname, course)"+
" VALUES" +
"(' " + username +" ' , ' " + name + " ' , ' " + surname + " ', ' " + course + " ') ";
rs = st.executeQuery(sqlStr);
while(rs.next()) {
studentBean stud = new studentBean();
stud.setusername(rs.getString(1));
}
}
}
catch (Exception e) {
try{out.println(sqlStr);out.println(e.getMessage() ); }
catch (Exception dum) {return null;}
}
}
}
------------------------------------
and here's the checkregister.jsp
<html>
<head></head>
<body>
<%@ page import="myBeans.studentBean" %>
<jsp:useBean id="xx" scope="session" class="myBeans.authBean2"/>
<% if (request.getParameter("username") != null) {
studentBean student;
student = xx.addStudent(request.getParameter("username"),
request.getParameter("name"),
request.getParameter("surname"),
request.getParameter("course"), out);
if (student != null) {
session.setAttribute("student", student);
%>
<p>Hello <%= ((studentBean) session.getAttribute("student")).getFullName()%>.
your User Name is:
<%= ((studentBean) session.getAttribute("student").getusername()%> <br/>
and your Registeration Number is:
<%= ((studentBean) session.getAttribute("student").getreg_num()%><br/>
use these information to login!.
</p>
<%
}
else {
String msg ="The user name already exists, please choose a different user name!";
request.setAttribute("msg",msg);
%>
<jsp:forward page="register.jsp"/>
<%
}
}
%>
</body>
</html>
----------------------------------
when i'm trying to compile the ant tool show this error message while compiling the authBean2.java
compile:
[javac] Compiling 3 source files to C:\apps\Project1\build\WEB-INF\classes
[javac] C:\apps\Project1\src\myBeans\authBean2.java:52: missing return state
ment
[javac] }
[javac] ^
[javac] 1 error
what's the problem?
|
|

May 30th, 2005, 08:51 PM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
here is my code have been complie vell!
<%@ page contentType="text/html;charset=gb2312"%>
<html><head><title>login</title></head>
<body>
<form name="login" action="check.jsp" method="post">
user£º<input type="text" name="admin"><br>
password£º<input type="password" name="password"><br>
<input type="submit" value="login"><br>
</form>
</body>
</html>
<%
String error=request.getParameter("error");
//error=new String(error.getBytes("8859_1"),"gb2312");
if(error==null) {}
else{
%>
<%=error%>
<%
}
%>
then
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="login.CheckBean"%>
<%
String admin = request.getParameter("admin");
String password = request.getParameter("password");
%>
<jsp:useBean id="checkBean" class="com.ice.login.CheckBean"/>
<jsp:setProperty name="checkBean" property="admin" value="<%= admin.trim() %>"/>
<jsp:setProperty name="checkBean" property="password" value="<%= password.trim() %>"/>
<%
String result = checkBean.checkIt();
if(result.equals("right£¡")){
session.setAttribute("admin",admin);
response.sendRedirect("main.jsp");
}
else
{
%>
<jsp:forward page="index.jsp">
<jsp:param name="error" value="<%=result%>"/>
</jsp:forward>
<%
}
%>
db.java
import java.sql.*;
public class DB {
private Connection conn;
private Statement stmt;
private ResultSet rs;
public DB() {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/news?user=admin&&password=");
stmt = conn.createStatement();
}
catch(Exception e) {
System.out.println(e);
}
}
public void update(String sql) {
try {
stmt.executeUpdate(sql);
}
catch(Exception e) {
System.out.println(e);
}
}
public ResultSet quarry(String sql) {
try {
rs = stmt.executeQuery(sql);
}
catch(Exception e) {
System.out.println(e);
}
return rs;
}
}
checkbean.java
import java.sql.*;
public class CheckBean {
private String message="",admin,password;
private DB db;
public CheckBean() {
db = new DB();
}
public void setAdmin(String admin){
this.admin = admin;
}
public void setPassword(String password) {
this.password = password;
}
public String checkIt() {
try {
ResultSet rs = db.quarry("select * from administrator where admin='"+this.admin+"'");
while(rs.next()){
String pws = rs.getString("password").trim();
if(pws.equals(this.password)){
message = "right£¡";
}
else message = "password wrong£¡";
return message;
}
message = "no this user£¡";
}
catch(Exception e) {
System.out.println(e);
}
return message;
}
}
if you have more program sent mail to me
|
|

June 2nd, 2005, 06:17 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi,
the addStudent() method returns an object of studentBean type
but the source code seems to b missing that return statment.
try to make change to addStudent() method as follows
public studentBean addStudent(String username,
String name, String surname, String course, JspWriter out) {
ResultSet rs = null;
Statement st = null;
String sqlStr = null;
try {
st = cn.createStatement();
sqlStr = " SELECT reg_num FROM student WHERE username = ' " + username + "' " ;
rs = st.executeQuery(sqlStr);
if (rs.next()) {
return null;
}
else {
st = cn.createStatement();
sqlStr = " INSERT INTO student " +
"(username, name, surname, course)"+
" VALUES" +
"(' " + username +" ' , ' " + name + " ' , ' " + surname + " ', ' " + course + " ') ";
rs = st.executeQuery(sqlStr);
studentBean stud = new studentBean();
while(rs.next()) {
stud.setusername(rs.getString(1));
}
return stud;
}
}
catch (Exception e) {
try{out.println(sqlStr);out.println(e.getMessage() ); }
catch (Exception dum) {return null;}
}
}
return null;
}
|
|
 |