当前位置:编程学习 > JAVA >>

eclipse 连接oracle数据库出错求大神指点

package step1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.io.*;
public class BaseDao {
public static String[] getProperties(){
String [] properties = new String[4];
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("DB.properties");
Properties prop = new Properties();
try {
prop.load(is);
properties[0] = prop.getProperty("classname");
properties[1] = prop.getProperty("url");
properties[2] =  prop.getProperty("uid");
properties[3] = prop.getProperty("pwd");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}

/**
 * 获取连接接口的方法
 * @return
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public static Connection getConnection() throws ClassNotFoundException, SQLException{
String ClassName = getProperties()[0];
String url = getProperties()[1];
String uid = getProperties()[2];
String pwd = getProperties()[3];
Class.forName(ClassName);
Connection con = DriverManager.getConnection(url, uid, pwd);
return con;
}

/**
 * 关闭connection和statement对象
 * @param con
 * @param st
 */
public static void closeAll(Connection con,Statement st){
if(st!=null){
try {
if(!st.isClosed()){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
if(!con.isClosed()){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

package step2;

public class Pet{
private int id;
private String name;
private String typename;
private int health;
private int love;
private String birthday;
private int owner_id;
private int store_id;
public Pet(int id, String name, String typename, int health, int love,
String birthday, int ownerId, int storeId) {
super();
this.id = id;
this.name = name;
this.typename = typename;
this.health = health;
this.love = love;
this.birthday = birthday;
owner_id = ownerId;
store_id = storeId;
}
@Override
public String toString() {
return "Pet [birthday=" + birthday + ", health=" + health + ", id="
+ id + ", love=" + love + ", name=" + name + ", owner_id="
+ owner_id + ", store_id=" + store_id + ", typename="
+ typename + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public int getOwner_id() {
return owner_id;
}
public void setOwner_id(int ownerId) {
owner_id = ownerId;
}
public int getStore_id() {
return store_id;
}
public void setStore_id(int storeId) {
store_id = storeId;
}

}

package step3;
public interface PetDao {
public void xianshi();
}


package step3;
import java.sql.*;
import step3.PetDao;
import step1.BaseDao;
public  class PetDaoImpl implements PetDao{
public void xianshi(){
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con=BaseDao.getConnection();
st = con.createStatement();
rs = st.executeQuery("select * from Pet");
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
String typename=rs.getString("typename");
int health=rs.getInt("health");
int love=rs.getInt("love");
String birthday=rs.getString("birthday");
int owner_id=rs.getInt("owner_id");
int store_id=rs.getInt("store_id");
System.out.println("宠物ID"+id+"宠物名"+name+"宠物类型"+typename+"健康值"+health+"爱心值"+love+"出生日期"+birthday+"宠物主人ID"+owner_id+"宠物商店ID"+store_id);
}
rs.close();
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}

package step5;
import java.util.*;
import step3.*;
import step2.*;
import step1.*;
public class Menu {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("宠物商店启动");
System.out.println("所有宠物从oracle中醒来!");
System.out.println("--------------------------------------");
PetDao pb = new PetDaoImpl();
pb.xianshi();

到这里就报一堆错。看不懂那里出问题了求大神指点
宠物商店启动
所有宠物从oracle中醒来!
--------------------------------------
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at step1.BaseDao.getProperties(BaseDao.java:15)
at step1.BaseDao.getConnection(BaseDao.java:34)
at step3.PetDaoImpl.xianshi(PetDaoImpl.java:11)
at step5.Menu.main(Menu.java:10)
Oracle Java --------------------编程问答-------------------- DB.properties要在工程目录下,另外,把这个配置文件发出来看看吧 --------------------编程问答-------------------- --------------------编程问答-------------------- 估计是 DB.properties 读取问题,贴个看看
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,