jdbc连接sql数据库时出现java.lang.NullPointerException异常 求大神解答 以下是源代码:
packagebishi;importjava.util.*;importjava.sql.*;publicclassShujuku{privatestaticConne...
package bishi;
import java.util.*;
import java.sql.*;
public class Shujuku {
private static Connection conn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
private static Statement st=null;
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=caipu";
String userName = "sa";
String userPwd = "093168";
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
System.out.print("123");
if(st==null)
st=conn.createStatement();
String sql="select * from quanbu";
rs=st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
} 展开
import java.util.*;
import java.sql.*;
public class Shujuku {
private static Connection conn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
private static Statement st=null;
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=caipu";
String userName = "sa";
String userPwd = "093168";
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
System.out.print("123");
if(st==null)
st=conn.createStatement();
String sql="select * from quanbu";
rs=st.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
} 展开
2个回答
展开全部
1、查看Connection是否获取成功
2、如果不成功,价差数据库能够正常连接(使用sql server客户端连接测试)
3、 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
查看驱动是否正确
2、如果不成功,价差数据库能够正常连接(使用sql server客户端连接测试)
3、 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
查看驱动是否正确
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
空指针是这么多错误中最好解决的!
断点调试,看看你出现空指针那行的变量哪个是NULL
就知道哪里没有给对象地址了
断点调试,看看你出现空指针那行的变量哪个是NULL
就知道哪里没有给对象地址了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询