JDBC连接数据库代码如下,总是跑出空指针异常!哪位同志看看! 5
packagecom.svse.util;importjava.io.IOException;importjava.io.InputStream;importjava.s...
package com.svse.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* 2011-2-18
*
* @author laoxu
*/
public class DBHelper {
// JDBC数据库连接属性字符串
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 初始化各种JDBC数据库连接参数
*
*/
private static void init() {
Properties props = new Properties();
InputStream is = DBHelper.class
.getResourceAsStream("/jdbcInfo.propertites");
try {
props.load(is);
driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
password = props.getProperty("jdbc.password");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获得数据库连接对象
*
* @return Connection
*/
public static Connection getConnection() {
Connection conn = null;
init();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭资源
*
* @param rs
* @param st
* @param conn
*/
public static void closeResource(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//测试数据库是否连接成功
Connection conn = DBHelper.getConnection();
if(conn != null){
System.out.println("数据库连接成功!");
}else{
System.out.println("数据库连接失败!");
}
}
}
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;database=tenement
jdbc.username=sa
jdbc.password=sa
错误:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.svse.util.DBHelper.init(DBHelper.java:38)
at com.svse.util.DBHelper.getConnection(DBHelper.java:55)
at com.svse.util.DBHelper.main(DBHelper.java:92) 展开
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* 2011-2-18
*
* @author laoxu
*/
public class DBHelper {
// JDBC数据库连接属性字符串
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 初始化各种JDBC数据库连接参数
*
*/
private static void init() {
Properties props = new Properties();
InputStream is = DBHelper.class
.getResourceAsStream("/jdbcInfo.propertites");
try {
props.load(is);
driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
username = props.getProperty("jdbc.username");
password = props.getProperty("jdbc.password");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获得数据库连接对象
*
* @return Connection
*/
public static Connection getConnection() {
Connection conn = null;
init();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭资源
*
* @param rs
* @param st
* @param conn
*/
public static void closeResource(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//测试数据库是否连接成功
Connection conn = DBHelper.getConnection();
if(conn != null){
System.out.println("数据库连接成功!");
}else{
System.out.println("数据库连接失败!");
}
}
}
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;database=tenement
jdbc.username=sa
jdbc.password=sa
错误:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at com.svse.util.DBHelper.init(DBHelper.java:38)
at com.svse.util.DBHelper.getConnection(DBHelper.java:55)
at com.svse.util.DBHelper.main(DBHelper.java:92) 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询