代码如下:import java.sql.*;
public class demo1 {
//连接数据库
public static void getDatabase(Connection con) throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
String url="jdbc:sqlserver://localhost:1433;DatabaseName=EMP";
con=DriverManager.getConnection(url, "sa", "123456");
} public static Statement getStmt(Connection con) throws SQLException{
return con.createStatement();
} public static ResultSet getRrs(Statement stmt,String sql) throws SQLException{
return stmt.executeQuery(sql);
} public static void show(ResultSet rrs) throws SQLException{
while(rrs.next()){
System.out.println(rrs.getInt("stuID")+"\t"+rrs.getString("stuName")+"\t"+
rrs.getString("stuJob")+"\t"+rrs.getString(4));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rrs = null;
try{
System.out.println("正在连接数据库.....");
//连接数据库
getDatabase(con);
System.out.println("连接数据库成功");
stmt=getStmt(con);
String sql="select stuID,stuName,stuJob,stuAdd from student";
rrs=getRrs(stmt,sql);
show(rrs);
}catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}finally{
try{
if(rrs!=null){
rrs.close();
rrs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}if(con!=null){
con.close();
con = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}}
出现异常:正在连接数据库.....
连接数据库成功
Exception in thread "main" java.lang.NullPointerException
at demo1.getStmt(demo1.java:10)
at demo1.main(demo1.java:32)
就是我加粗的两句出现的异常!我才学这个!不知道怎么解决,.NullPointerException是空指针异常类 但是con不是null啊!请教高手指教!
public class demo1 {
//连接数据库
public static void getDatabase(Connection con) throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
String url="jdbc:sqlserver://localhost:1433;DatabaseName=EMP";
con=DriverManager.getConnection(url, "sa", "123456");
} public static Statement getStmt(Connection con) throws SQLException{
return con.createStatement();
} public static ResultSet getRrs(Statement stmt,String sql) throws SQLException{
return stmt.executeQuery(sql);
} public static void show(ResultSet rrs) throws SQLException{
while(rrs.next()){
System.out.println(rrs.getInt("stuID")+"\t"+rrs.getString("stuName")+"\t"+
rrs.getString("stuJob")+"\t"+rrs.getString(4));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rrs = null;
try{
System.out.println("正在连接数据库.....");
//连接数据库
getDatabase(con);
System.out.println("连接数据库成功");
stmt=getStmt(con);
String sql="select stuID,stuName,stuJob,stuAdd from student";
rrs=getRrs(stmt,sql);
show(rrs);
}catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}finally{
try{
if(rrs!=null){
rrs.close();
rrs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}if(con!=null){
con.close();
con = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}}
出现异常:正在连接数据库.....
连接数据库成功
Exception in thread "main" java.lang.NullPointerException
at demo1.getStmt(demo1.java:10)
at demo1.main(demo1.java:32)
就是我加粗的两句出现的异常!我才学这个!不知道怎么解决,.NullPointerException是空指针异常类 但是con不是null啊!请教高手指教!
解决方案 »
- java与delphi中数据类型的对应关系
- 谁有JAVA2核心技术第1卷这本书第七版的配套示例压缩包?
- 帮忙修改一下一个小程序(偶自己不懂JAVA),谁帮我忘APPLET显示的计算器上多显示一行文本?
- 求页面中APPLET弹出窗口解决方案
- JAVA 线程,主程序循环调用多线程,在线等待!
- java客户端与c++服务器端的一些问题
- 关于按钮一个简单的问题请教各位朋友,谢谢
- 在jbuilder 里面能够获取数据库数据,导成.exe文件后就不能获取了,怎么回事啊??
- 怎么连不上SOHU的POP服务器???
- 有谁能讲讲关于io方面的知识?80分
- 网络连接问题,高分求解啊,在线等待···
- wait阻塞之后能执行下面的notify()吗
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rrs = null;
try{
System.out.println("正在连接数据库.....");
//连接数据库
getDatabase(con);//做完这个事情后,con还是null,
将getDatabase修改一下:public static Connection getDatabase() throws ClassNotFoundException, SQLException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
String url="jdbc:sqlserver://localhost:1433;DatabaseName=EMP";
Connection con=DriverManager.getConnection(url, "sa", "123456");
return con;
}
main方法:
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rrs = null;
try{
System.out.println("正在连接数据库.....");
//连接数据库
con = getDatabase(con);
...
at demo1.main(demo1.java:32)
这两行的代码是什么?贴出来看看。或者是加个断点跟踪看看。
谢谢!
问题解决了!
调用getDatabase()方法的时候包con=null传进去了不过传过来的con(实参:调用处的变量,此时指向堆上地址:null)到了方法中,已经是另外一个栈上面的的地址了(形参:方法中的局部变量,此时指向堆上地址:null)在方法中形参的引用地址被改变了 con=DriverManager.getConnection(url, "sa", "123456");
而实参的引用地址未被改变 ,依然是null值