求解,急急急
package 底层数据库搭建;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB {
private static final String url="jdbc:mysql://127.0.0.1:3306/t1";
private static final String user="root";
private static final String password="1934jy26";
private static Connection con=null;
static//首先执行静态块
{
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception
{
if(con==null)
{
Connection con=DriverManager.getConnection(url, user, password);
System.out.println("数据库连接正常");
}
else
{
System.out.println("数据库连接不正常");
}
return con;
}
// public static void main(String []args) throws Exception
// {
// Connection con=DB.getConnection();
// }
}
package 对象层;
import java.sql.Date;
public class Goddess {
private int id;
private String username;
private int sex;
private int age;
private Date bicreate_userrthday;
private String email;
private String mobile;
private String create_user;
private String update_user;
private Date create_date;
private Date update_date;
private int isdel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBicreate_userrthday() {
return bicreate_userrthday;
}
public void setBicreate_userrthday(Date bicreate_userrthday) {
this.bicreate_userrthday = bicreate_userrthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getCreate_user() {
return create_user;
}
public void setCreate_user(String create_user) {
this.create_user = create_user;
}
public String getUpdate_user() {
return update_user;
}
public void setUpdate_user(String update_user) {
this.update_user = update_user;
}
public Date getCreate_date() {
return create_date;
}
public void setCreate_date(Date create_date) {
this.create_date = create_date;
}
public Date getUpdate_date() {
return update_date;
}
public void setUpdate_date(Date update_date) {
this.update_date = update_date;
}
public int getIsdel() {
return isdel;
}
public void setIsdel(int isdel) {
this.isdel = isdel;
}
}
package 方法层;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import 对象层.Goddess;
import 底层数据库搭建.DB;
public class Goddess_Action {
private static Connection con=null;
public void add_Goddess()
{
}
public void update_Goddess()
{
}
public void delete_Goddess()
{
}
public List<Goddess> query() throws Exception//获取所有的对象
{
//连接数据库
Connection con=DB.getConnection();
Statement s=con.createStatement(); //此处显示有问题
//对数据库操作并且获得对数据库的操作结果
ResultSet r=s.executeQuery("select age,user_name from imooc_goddess");
List<Goddess> l1=new ArrayList<Goddess>();
Goddess g=null;
//判并且输出结果
while(r.next())//bollean类型的值,不断地循环,光标最初是在第一行
{
//实例化每个Goddess对象,并且把从数据库查询得到的值加入到其中
g=new Goddess();
g.setUsername(r.getString("user_name"));
g.setAge(r.getInt("age"));
l1.add(g);
}
return l1;
}
public Goddess getGoddess()//获取单个的对象
{
return null;
}
}
package 控制层;
import java.util.List;
import 对象层.Goddess;
import 方法层.Goddess_Action;
public class Contral {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Goddess_Action g1=new Goddess_Action();
List<Goddess> l1=g1.query(); //此处显示有问题
}
}
运行结果:
数据库连接正常
Exception in thread "main" java.lang.NullPointerException
at 方法层.Goddess_Action.query(Goddess_Action.java:30)
at 控制层.Contral.main(Contral.java:12)
求解,急急急,谢谢