查看: 887|回复: 8
|
我的RoomBean.java可以run但是RoomDataBean.java不可以compile
[复制链接]
|
|
我的RoomBean.java可以run但是RoomDataBean.java不可以compile
以下是我的RoomBean.java和RoomDataBean.java,我的RoomBean.java可以run但是RoomDataBean.java不可以compile.问题出在那里呢?
这是RoomBean.java 的code
package dbentity;
public class RoomBean
{
private String
roomID,roomType,checkIn,checkOut,numberOfDay,specialFacilities;
public void setRoomID(String id)
{
roomID = id;
}
public String getRoomID()
{
return roomID;
}
public void setRoomType(String type)
{
roomType = type;
}
public String getRoomType()
{
return roomType;
}
public void setCheckIn(String day)
{
checkIn = day;
}
public String getCheckIn()
{
return checkIn;
}
public void setCheckOut(String day)
{
checkOut = day;
}
public String getCheckOut()
{
return checkOut;
}
public void setNumberOfDay(String number)
{
numberOfDay = number;
}
public String getNumberOfDay()
{
return numberOfDay;
}
public void setSpecialFacilities(String facilities)
{
specialFacilities = facilities;
}
public String getSpecialFacilities()
{
return specialFacilities;
}
}
这是RoomDataBean.java(有问题)
package dbentity;
import java.io.*;
import java.sql.*;
import java.util.*;
public class RoomDataBean
{
private Connection connection;
private PreparedStatement addRecord, getRecords;
private Statement statement;
public RoomDataBean() throws Exception
{
System.setProperty("jdbc.odbc.system.home","c:/Microsoft Access");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:Room Accommodation.mdb");
statement = connection.createStatement();
}
public ArrayList getRoomList() throws SQLException
{
ArrayList roomList = new ArrayList();
ResultSet results =statement.executeQuery(
"SELECT roomID,roomType,checkIn,checkOut,numberOfDay,specialFacilities FROM room");
while (results.next() )
{
RoomBean room = new RoomBean();
room.setRoomID( results.getString(1) );
room.setRoomType( results.getString(2) );
room.setCheckIn( results.getString(3) );
room.setCheckOut( results.getString(4) );
room.setNumberOfDay( results.getString(5) );
room.setSpecialFacilities( results.getString(6) );
roomList.add(room);
}
return roomList;
}
public void addRoom(RoomBean room ) throws SQLException
{
statement.executeUpdate( " INSERT INTO room (roomID,roomType,checkIn,checkOut,numberOfDay,specialFacilities) VALUES ( ' " +room.getRoomID() +" ',' "+room.getRoomType()+" ',' "+room.getCheckIn()+ " ',' "+room.getCheckOut()+ " ',' "+room.getNumberOfDay()+" ',' "+room.getSpecialFacilities()+" ')");
}
protected void finalize()
{
try{
statement.close();
connection.close();
}
catch(SQLException sqlException){
sqlException.printStackTrace();
}
}
}
请各位大大帮帮忙,感激不尽。。。 |
|
|
|
|
|
|
|
发表于 4-11-2005 11:01 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 4-11-2005 11:55 AM
|
显示全部楼层
有啊,就是compile不到。有几个
RoomBean room = new RoomBean();
public void addRoom(RoomBean room ) throws SQLException |
|
|
|
|
|
|
|
发表于 4-11-2005 12:10 PM
|
显示全部楼层
while Loop create 同样名字的obj..
我想这是问题所在吧..
while (results.next() )
{
RoomBean room = new RoomBean();
...
roomList.add(room);
}
return roomList;
} |
|
|
|
|
|
|
|
楼主 |
发表于 4-11-2005 12:38 PM
|
显示全部楼层
这样的话 public void addRoom(RoomBean room ) throws SQLException 有问题吗? |
|
|
|
|
|
|
|
发表于 4-11-2005 11:32 PM
|
显示全部楼层
Javabean & Data Access Layer
1。RoomBean 就取名为 Room。以 J2EE 的标准,人人都知道它是一个 Javabean。
2。这个 Javabean 是一个 value object,记得要 implement serializable。(this is one of the J2EE best practices)。如果没有 implement serializable,在 single server 里操作当然不会有问题。但是未来这个 application 要 deploy 到 cluster environment 时候,它就不能正常操作了。在 value object 里 implement serializable 也只是花你几秒的时间吧。。。
3。Data Access 方面用 DAO (Data Access Object) Pattern。以下的 code 是比较简单的 DAO 方式:
Connection.properties file
- DriverName=com.mysql.jdbc.Driver
- URL=jdbc:mysql://yourdomain.com
- Port=3306
- DB=ej605c14
- UserName=ej605c14
- Password=bob_the_builder
复制代码
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * This class provides Course related JDBC operations.
- *
- * @author Seagull Ng
- */
- public class CourseDAO
- {
- /**
- * strSqlGetCourse - Query string to get a course information by courseId.
- */
- private static final String strSqlGetCourse = "SELECT * FROM A2Courses WHERE courseId = ?";
- /**
- * strSqlGetCourses - Query string to get all courses.
- */
- private static final String strSqlGetCourses = "SELECT * FROM A2Courses";
- /**
- * strSqlInsertCourse - Query string to insert a course.
- */
- private static final String strSqlInsertCourse = "INSERT INTO A2Courses VALUES (?, ?, ?, ?, ?)";
- /**
- * This method gets a course by matching a course id.
- *
- * @param courseId Course ID
- * @return Course object populated with course information
- * @exception SQLException
- */
- public static Course getCourse(String courseId)
- {
- Course course = new Course();
- PreparedStatement stmt = null;
- ResultSet rs = null;
- try
- {
- Connection conn = J2EEServices.getConnection();
- stmt = conn.prepareStatement(strSqlGetCourse);
- stmt.setString(1, courseId);
- stmt.execute();
- rs = stmt.getResultSet();
- while (rs.next())
- {
- course.setCourseId(rs.getString("courseId"));
- course.setCourseName(rs.getString("courseName"));
- course.setCourseDesc(rs.getString("courseDesc"));
- course.setCoursePeriod(rs.getString("coursePeriod"));
- course.setCoursePrice(rs.getDouble("coursePrice"));
- }
- J2EEServices.closeDBResources(rs, stmt, conn);
- }
- catch (SQLException e)
- {
- System.err.println("SQL Exception in CourseDAO.getCourse(): " + e);
- }
- return course;
- }
- /**
- * This method gets all courses.
- *
- * @return a collection of Course objects in a Map
- * @exception SQLException
- */
- public static List getCourses()
- {
- List courses = new ArrayList();
- Statement stmt = null;
- ResultSet rs = null;
- try
- {
- Connection conn = J2EEServices.getConnection();
- stmt = conn.createStatement();
- stmt.execute(strSqlGetCourses);
- rs = stmt.getResultSet();
- while (rs.next())
- {
- Course course = new Course();
- course.setCourseId(rs.getString("courseId"));
- course.setCourseName(rs.getString("courseName"));
- course.setCourseDesc(rs.getString("courseDesc"));
- course.setCoursePeriod(rs.getString("coursePeriod"));
- course.setCoursePrice(rs.getDouble("coursePrice"));
- courses.add(course);
- }
- J2EEServices.closeDBResources(rs, stmt, conn);
- }
- catch (SQLException e)
- {
- System.err.println("SQL Exception in CourseDAO.getCourses(): " + e);
- }
- return courses;
- }
-
- /**
- * This method inserts a course record into the table.
- *
- * @param courseId Course ID
- * @param courseName Course Name
- * @param courseDesc Course Description
- * @param coursePeriod Course Period (eg. Fall 2005)
- * @param coursePrice Course Price
- * @return either the row count for INSERT, or 0 for SQL statements that return nothing
- * @exception SQLException
- */
- public static int insertCourse(String courseId, String courseName, String courseDesc, String coursePeriod, double coursePrice)
- {
- int res = 0;
- PreparedStatement stmt = null;
- ResultSet rs = null;
-
- try
- {
- Connection conn = J2EEServices.getConnection();
- stmt = conn.prepareStatement(strSqlInsertCourse);
- stmt.setString(1, courseId);
- stmt.setString(2, courseName);
- stmt.setString(3, courseDesc);
- stmt.setString(4, coursePeriod);
- stmt.setDouble(5, coursePrice);
- res = stmt.executeUpdate();
- J2EEServices.closeDBResources(rs, stmt, conn);
- }
- catch (SQLException e)
- {
- System.err.println("SQL Exception in CourseDAO.insertCourse(): " + e);
- }
-
- return res;
- }
- }
复制代码 |
|
|
|
|
|
|
|
发表于 4-11-2005 11:49 PM
|
显示全部楼层
原帖由 jasonmun 于 4-11-2005 12:10 PM 发表
while Loop create 同样名字的obj..
我想这是问题所在吧..
while (results.next() )
{
RoomBean room = new RoomBean();
...
roomList.add(room);
}
retu ...
每 loop 一次就产生一个新的 room,然后把已经存值的 room 放到一个 collection 里。这样会有什么问题? |
|
|
|
|
|
|
|
楼主 |
发表于 5-11-2005 07:55 PM
|
显示全部楼层
while (results.next() )
{
RoomBean room = new RoomBean();
...
roomList.add(room);
}
return roomList;
}
我也是跟着以上的code来写呀但RoomBean room = new RoomBean() 就是有error。RoomDataBean.java:50:cannot resolve symbol。Symbol : class RoomBean. |
|
|
|
|
|
|
|
楼主 |
发表于 5-11-2005 08:12 PM
|
显示全部楼层
是package 的问题吗?一定要用package 吗?如果是应该如何写呢?
这个是connection 不是package,是吗?
package dbentity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection{
String strUrl = "";
Connection conn = null;
public DBConnection()
{
}
public Connection getMicrosoftAccessConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
this.strUrl = "jdbcdbc:Microsoft Access:Room Accommodation.mdb";
this.conn = DriverManager.getConnection( strUrl,"root", "" );
System.out.println("URL: " + strUrl);
System.out.println("Connection: " + conn);
}
catch( ClassNotFoundException cnfe )
{
cnfe.printStackTrace();
}
catch( SQLException sqle )
{
sqle.printStackTrace();
}
return conn;
}
} |
|
|
|
|
|
|
| |
本周最热论坛帖子
|