问题域类与数据访问类的交互
这是课本上的一个程序。
public class User {
private String userID;
private String name;
private String password;
public User(String useID,String name,String password){
setName(name);
setUserID(userID);
setPassword(password);
}
public void setName(String name){this.name=name;}
public String getName(){return name;}
public void setUserID(String userID){this.userID=userID;}
public String getUserID(){return userID;}
public void setPassword(String password){this.password=password;}
public String getPassword(){return password;}
public String getDetails(){
String info;
info="UserID:"+getUserID()+";\n姓名:"+getName()+",\n密码:"+getPassword();
return info;
}
public static void initialize(){UserDA.initialize();}
public static void terminate(){UserDA.terminate();}
public static User find(String userID)throws NotFoundException{return UserDA.find(userID);}
public void add() throws DuplicateException{UserDA.add(this);}
public void delete() throws NotFoundException{UserDA.delete(this);}
public void update() throws NotFoundException{UserDA.update(this);}
}
import java.sql.*;
public class UserDA {
static User aUser;
static String url="jdbc:odbc:myDataSource";
static Connection aConnection;
static Statement aStatement;
static String userID;
static String name;
static String password;
public static Connection initialize(){
try{
Class.forName("sun.jdbc.odbc.JdbOdbcDriver");
aConnection=DriverManager.getConnection(url,"","");
aStatement=aConnection.createStatement();
}
catch(ClassNotFoundException e){
System.out.println(e);
}
catch(SQLException e){
System.out.println(e);
}
return aConnection;
}
//释放所用系统资源
public static void terminate(){
try{
aStatement.close();
aConnection.close();
}
catch(SQLException e){
System.out.println(e);
}
}
//添加一个新纪录
public static void add(User aUser) throws DuplicateException{
name=aUser.getName();
userID=aUser.getUserID();
password=aUser.getPassword();
String sql="INSERT INTO UserT(UserID,Name,Password)"+"VALUES('"+userID+"','"+name+"','"+password+"')";
//System.out.println(sql);
try{
User c=find(userID);
throw(new DuplicateException("该用户已存在!"));
}
catch(NotFoundException e){
try{
int result=aStatement.executeUpdate(sql);
}
catch(SQLException ee){
System.out.println(ee);
}
}
}
//删除指定的记录
public static void delete(User aUser){
userID=aUser.getUserID();
String sql="DELETE FROM UserT"+"WHERE userID='"+userID+"'";
//System.out.println(sql);
try{
int result=aStatement.executeUpdate(sql);
}
catch(SQLException e){
System.out.println(e);
}
}
public static void update(User aUser)throws NotFoundException{
name=aUser.getName();
userID=aUser.getUserID();
password=aUser.getPassword();
String sql="Update User SET Name='"+name+"',"+"Password='"+password+"'"+"WHERE userID='"+userID+"'";
//System.out.println(sql);
try{
User c=find(userID);
int result=aStatement.executeUpdate(sql);
}
catch(SQLException e){
System.out.println(e);
}
}
//检索特定的属性值
public static User find(String key)throws NotFoundException{
aUser=null;
String sql="SELECT userID,Name,password,password FROM userT WHERE userID='"+key+"'";
try{
ResultSet rs=aStatement.executeQuery(sql);
boolean gotIt=rs.next();
if(gotIt){
userID=rs.getString(1);
name=rs.getString(2);
password=rs.getString(3);
aUser=new User(userID,name,password);
}
else throw (new NotFoundException("没有发现这个记录!"));
rs.close();
}
catch(SQLException e){
System.out.println(e);
}
return aUser;
}
}
public class DuplicateException extends Exception{
public DuplicateException(String message){
super(message);
}
}
public class TesterUserAndUserDA {
public static void main(String []args){
User firstUser=new User("SW3333","Liping","12345678");
User.initialize();
//test add
try{
firstUser.add();
System.out.println("加一个用户");
}
catch(DuplicateException e){
System.out.println(e);
}
//test find
try{
firstUser=User.find("SW3333");
System.out.println("查询"+firstUser.getDetails());
}
catch(NotFoundException e){
System.out.println(e);
}
//test update
try{
firstUser=User.find("SW3333");
firstUser.setPassword("88888888");
firstUser.update();
firstUser=User.find("SW3333");
System.out.println("更新后"+firstUser.getDetails());
}
catch(NotFoundException e){
System.out.println(e);
}
//test delete
try{
firstUser.delete();
System.out.println("要删除"+firstUser.getDetails());
firstUser=User.find("SW3333");
System.out.println("删除后查询"+firstUser.getDetails());
}
catch(NotFoundException e){
System.out.println(e);
}
User.terminate();
}
}
在程序所在文件夹中,有建立一个userT的数据库,其中有一个userT的数据表。但是就是不能运行。
显示:java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbOdbcDriver
Exception in thread "main" java.lang.NullPointerException
at UserDA.find(UserDA.java:100)
at UserDA.add(UserDA.java:51)
at User.add(User.java:29)
[最优解释]
String password=mes3Text.getText();
firstUser=new User(userID,name,password);
User.initialize();
try{
firstUser.add();
System.out.println("加一个用户");
}
catch(DuplicateException ee){
System.out.println(ee);
}
}
if(e.getSource()==findBtn) {
//test find
try{
String userID=mes1Text.getText();
firstUser=User.find(userID);
System.out.println("查询"+firstUser.getDetails());
}
catch(NotFoundException ee){
System.out.println(ee);
}}
if(e.getSource()==updBtn) {
//test update
try{
String userID=mes1Text.getText();
String name=mes2Text.getText();
String password=mes3Text.getText();
firstUser.update();
firstUser=User.find(userID);
System.out.println("更新后"+firstUser.getDetails());
}
catch(NotFoundException ee){
System.out.println(ee);
}
}
if(e.getSource()==delBtn) {
//test delete
try{String userID=mes1Text.getText();
firstUser.delete();
System.out.println("要删除"+firstUser.getDetails());
firstUser=User.find(userID);
System.out.println("删除后查询"+firstUser.getDetails());
}
catch(NotFoundException ee){
System.out.println(ee);
}}
if(e.getSource()==closeBtn) {System.exit(0);}
User.terminate();
}
}
[其他解释]
貌似没什么大问题。
就是要注意firstUser的初始化问题,因为你将它定义为成员变量了,有时(如update之前)没有做初始化。