java执行数据库查询报错

package sql;
import java.sql.*;
//import javax.xml;

public class DBC {
    static Connection con;
    static Statement sql;
    static ResultSet res;
    public static void main(String []args) throws SQLException {
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SA0326";
        String userName="sa";            
        String userPwd="123456";
     try {
         Class.forName(driverName);  
//         System.out.println("加载SQLServer驱动类成功!");
    }catch(ClassNotFoundException a){
        System.out.println("加载SQLServer驱动失败!");
        a.printStackTrace();
}
     Connection dbcon=null;           
     try{
         dbcon=DriverManager.getConnection(dbURL,userName,userPwd);
//         System.out.println("数据库连接成功!");
         dbcon.close();
    } catch(SQLException e){
        System.out.println("数据库连接失败!");
        e.printStackTrace();
    }
     try {
            sql = con.createStatement();
            res = sql.executeQuery("select * from rlb");
            while (res.next()) {
                String id =res.getString("time_id");
                String date =res.getString("the_date");
                String day =res.getString("the_day");
                String mo =res.getString("the_month");
                String ye =res.getString("the_year");
                String ofmo =res.getString("day_of_month");
                String week =res.getString("week_of_year");
                String ofye =res.getString("month_of_year");
                String qu =res.getString("quarter");
                String per =res.getString("fiscal_period");
                String leday =res.getString("legal_holiday");
                System.out.print(id);
                System.out.print(date);
                System.out.print(day);
                System.out.print(mo);
                System.out.print(ye);
                System.out.print(ofmo);
                System.out.print(week);
                System.out.print(ofye);
                System.out.print(qu);
                System.out.print(per);
                System.out.print(leday);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
     }
    }
--报错信息

java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "sql.DBC.con" is null
    at sql.DBC.main(DBC.java:31)

你这个 con 不对吧,不是应该用 dbcon 吗,con 你都从来没有初始化过。

报错已经很明显了 con这个配置里面有空值 看一下配置

怎么看配置,新手不知道。。。。

con这个配什么值好啊

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632