老是提示我空指针异常,不知道这样连接数据库是不是可以
请教下各位
数据库驱动代码如下
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil
{
private static Connection conn;
public DBUtil(){}
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/things_store?
user=root");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public Connection getConn() {
return conn;
}
public static Connection getConnection()
{
return conn;
}
}
//被测试方法代码如下:
package dao;
import dao.CatalogDAO;
import dao.DBUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Catalog;
import model.Things;
public class CatalogDAOImpl implements CatalogDAO{
/** Creates a new instance of CatalogDAOImpl */
public CatalogDAOImpl() {
}
public void add(Catalog c) {
//DBUtil db = new DBUtil();
try {
PreparedStatement ps = DBUtil.getConnection().prepareStatement("INSERT INTO catalog(name) VALUES(?)");
ps.setString(1,c.getName());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void update(Catalog c) {
//DBUtil db = new DBUtil();
try {
PreparedStatement ps = DBUtil.getConnection().prepareStatement("UPDATE catalog SET name=? WHERE id=?");
ps.setString(1,c.getName());
ps.setInt(2,c.getID());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void delete(Catalog c) {
//DBUtil db = new DBUtil();
try {
PreparedStatement ps = DBUtil.getConnection().prepareStatement("DELETE from catalog WHERE id=?");
ps.setInt(1,c.getID());
ps.executeUpdate();
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public Catalog get(int id) {
Catalog c = null;
//DBUtil db = new DBUtil();
PreparedStatement ps;
try {
c = new Catalog();
ps = DBUtil.getConnection().prepareStatement("SELECT * FROM catalog WHERE id=?");
ps.setInt(1,id);
ResultSet rs= ps.executeQuery();
if(rs.next())
{
c.setID(rs.getInt(1));
c.setName(rs.getString(2));
}
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return c;
}
public ArrayList getCatalog() {
ArrayList al = new ArrayList();
Statement s;
try {
s = DBUtil.getConnection().createStatement();
ResultSet rs= s.executeQuery("SELECT * FROM catalog");
while (rs.next())
{
Catalog c = new Catalog();
c.setID(rs.getInt(1));
c.setName(rs.getString(2));
al.add(c);
}
s.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return al; //按传说中的命名规则,应该改为result;
}
public ArrayList getThings(Catalog c) {
ArrayList al = new ArrayList();
//DBUtil db = new DBUtil();
PreparedStatement ps;
try {
ps = DBUtil.getConnection().prepareStatement("SELECT * FROM catalog WHERE id=?");
ps.setInt(1,c.getID());//设置第一个位置
ResultSet rs= ps.executeQuery();
while (rs.next())
{
Things thing = new Things();
thing.setID(rs.getInt(1));
thing.setTitle(rs.getString(2));
thing.setAuthor(rs.getString(3));
thing.setPrice(rs.getDouble(4));
thing.setContent(rs.getString(5));
thing.setPicture(rs.getString(6));
thing.setPostTime(rs.getDate(7));
thing.setClicks(rs.getInt(8));
al.add(thing);
}
ps.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return al; //按传说中的命名规则,应该改为result;
}
}