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)