问题域类与数据访问类的交互
这是课本上的一个程序。
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)
[最优解释]
兄弟,JdbcOdbcDriver类名写错了吧。
[其他解释]
对呢!谢谢!改过来了之后显示的是:
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
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)
at TesterUserAndUserDA.main(TesterUserAndUserDA.java:9)
然后我去百度配置了数据源,但是显示:
java.sql.SQLException: No data found
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)
at TesterUserAndUserDA.main(TesterUserAndUserDA.java:9)
这是为什么啊?
[其他解释]
你可以继续百度啊。应该是特定数据库中的数据访问操作的次序/次数问题。
一开始就尝试这么多的代码太多了,你可以从少一点的代码一点一点地试着看看。
[其他解释]
现在弄好了,是因为数据源配置出了问题。现在解决掉了,谢谢!
可是要把test程序改成GUI类,我的运行不了。。。前面学GUI的时候就没有做练习,所以都不知道错误是什么。。。可以帮忙看一下吗?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TesterUserAndUserDA extends JFrame implements ActionListener {
User firstUser;
JTextField mes1Text,mes2Text,mes3Text,mes4Text,mes5Text;
JTextArea out;
JButton addBtn,findBtn,updBtn,delBtn,closeBtn;
public TesterUserAndUserDA(){
super("数据访问");
Container c=this.getContentPane();
c.setLayout(new GridLayout(3,6));
JPanel first=new JPanel(new FlowLayout());
JPanel second=new JPanel(new FlowLayout());
JPanel third=new JPanel(new FlowLayout());
addBtn=new JButton("添加");
findBtn=new JButton("查找");
updBtn=new JButton("修改");
delBtn=new JButton("删除");
closeBtn=new JButton("退出");
JLabel mes1=new JLabel("UserID:");
mes1Text=new JTextField(10);
JLabel mes2=new JLabel("Name:");
mes1Text=new JTextField(10);
JLabel mes3=new JLabel("Password:");
mes1Text=new JTextField(10);
first.add(mes1);
first.add(mes1Text);
first.add(mes2);
first.add(mes2Text);
first.add(mes3);
first.add(mes3Text);
c.add(first);
second.add(addBtn);
second.add(findBtn);
second.add(updBtn);
second.add(delBtn);
second.add(closeBtn);
c.add(second);
JLabel output=new JLabel("显示:");
out=new JTextArea();
third.add(output);
third.add(out);
c.add(third);
addBtn.addActionListener(this);
findBtn.addActionListener(this);
updBtn.addActionListener(this);
delBtn.addActionListener(this);
closeBtn.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1000,1000);
this.setVisible(true);
}
public static void main(String []args){
TesterUserAndUserDA bUser=new TesterUserAndUserDA();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==addBtn) {
String userID=mes1Text.getText();
String name=mes2Text.getText();
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之前)没有做初始化。