标题:新手求教,空指针异常
只看楼主
stm32
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-4-30
结帖率:0
已结贴  问题点数:20 回复次数:2 
新手求教,空指针异常
package model;

import java.util.Date;

public class Goddess {
    private Integer id;
    private String user_name;
    private Integer sex;
    private Integer age;
    private Date birthday;
    private String mobile;
    private String update_user;
    private String create_user;
    private Date create_date;
    private Date update_date;
    private Integer isdel;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getUpdate_user() {
        return update_user;
    }

    public void setUpdate_user(String update_user) {
        this.update_user = update_user;
    }

    public String getCreate_user() {
        return create_user;
    }

    public void setCreate_user(String create_user) {
        this.create_user = create_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 Integer getIsdel() {
        return isdel;
    }

    public void setIsdel(Integer isdel) {
        this.isdel = isdel;
    }

}


package db;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

    public class DButil {
        private static final String URL="jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=gbk";
        private static final String USER="root";
        private static final String PASSWORD= "940616";
        private static Connection conn=null;
static {
            // 1.加载驱动
try {
    Class.forName("com.mysql.jdbc.Driver");
    //获得数据库的连接(创建对象)
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 
     }
  
  public static Connection getConnection(){
    return conn;   
  }
        public static void main(String[] args) throws Exception {
            // 1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得数据库的连接(创建对象)
         conn=DriverManager.getConnection(URL, USER, PASSWORD);
            //3.通过数据库的连接操作,实现增删改查
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select user_name,age from imooc_goddess");
              System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
              }
        
        }   
        
   


        

package action;

import java.util.List;

import model.Goddess;

import dao.GoddessDao;
public class GoddessAction {
      public static  void main(String[] args) throws Exception {
          GoddessDao g=new GoddessDao();
         
         List<Goddess> gs=g.query();
          for(Goddess goddess :gs){
              System.out.println(goddess.getUser_name()+","+goddess.getAge());
          }
         
         
      }
}
package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import db.DButil;
import model.Goddess;

public class GoddessDao {
    public void addGoddess() {
         DButil.getConnection();
    }

    public void updateGoddess() {

    }

    public void delGoddess() {

    }
    public List<Goddess> query() throws SQLException {     
        Connection conn = DButil.getConnection();     
        Statement stmt=conn.createStatement();
        ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
        List<Goddess> gs=new ArrayList<Goddess>();
            Goddess g = null;
            while (rs.next()) {
                g = new Goddess();
                g.setUser_name(rs.getString("user_name"));
                g.setAge(rs.getInt("age"));
               
                gs.add(g);
        }
            return gs;

        }
    public Goddess get() {
        return null;
        
    }
}
搜索更多相关主题的帖子: private package mobile public return 
2017-04-30 21:13
stm32
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-4-30
得分:0 
标注红色的是提示指针错误的地方
2017-04-30 21:13
漫步云端24
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:47
专家分:347
注 册:2017-4-24
得分:20 
会使debug 这种情况最好debug走一遍 看一下是连接的问题,还是执行查询的问题
2017-05-02 09:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-476553-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068812 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved