错误源代码如下:
// Java Document
import java.sql.*;
public class JdbcTest2
{
public static void main(String args[])
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("create JdbcOdbcBridge error");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
}
catch(SQLException e)
{
System.out.println("creat connection error");
}
try
{
sql = con.createStatement();
}
catch(SQLException e)
{
System.out.println("createStatement error");
}
}
}
用javac编译 报错:JdbcTest2.java:29: variable con might not have been initialized
sql = con.createStatement();
^
1 errors正确的源代码如下:
// Java Document
import java.sql.*;
public class JdbcTest2
{
public static void main(String args[])
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("create JdbcOdbcBridge error");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
sql = con.createStatement();
con.close();
}
catch(SQLException e1)
{
System.out.println("error");
}
}
}1.我想问为什么把上诉错误源代码中的后两个try合并成一个try就正确了,分开捕获异常就不行吗,而且报错connection对象没有初始化
2.若把上述第二个正确正确源代码中的con.close();从try中抽出来单独写在后面,即改成如下形式:
......
......
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
sql = con.createStatement();
}
catch(SQLException e1)
{
System.out.println("error");
}
con.close;
......
......那么用javac编译也同样报错:
JdbcTest2.java:28: variable con might not have been initialized
con.close();
^
JdbcTest2.java:28: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
con.close();
^
2 errors这又是为什么
// Java Document
import java.sql.*;
public class JdbcTest2
{
public static void main(String args[])
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("create JdbcOdbcBridge error");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
}
catch(SQLException e)
{
System.out.println("creat connection error");
}
try
{
sql = con.createStatement();
}
catch(SQLException e)
{
System.out.println("createStatement error");
}
}
}
用javac编译 报错:JdbcTest2.java:29: variable con might not have been initialized
sql = con.createStatement();
^
1 errors正确的源代码如下:
// Java Document
import java.sql.*;
public class JdbcTest2
{
public static void main(String args[])
{
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("create JdbcOdbcBridge error");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
sql = con.createStatement();
con.close();
}
catch(SQLException e1)
{
System.out.println("error");
}
}
}1.我想问为什么把上诉错误源代码中的后两个try合并成一个try就正确了,分开捕获异常就不行吗,而且报错connection对象没有初始化
2.若把上述第二个正确正确源代码中的con.close();从try中抽出来单独写在后面,即改成如下形式:
......
......
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
sql = con.createStatement();
}
catch(SQLException e1)
{
System.out.println("error");
}
con.close;
......
......那么用javac编译也同样报错:
JdbcTest2.java:28: variable con might not have been initialized
con.close();
^
JdbcTest2.java:28: unreported exception java.sql.SQLException; must be caught or
declared to be thrown
con.close();
^
2 errors这又是为什么
报第一种错误
String strAA ;
try {
strAA = "";
} catch(Exception e){}
try {
System.out.println(strAA);
} catch(Exception e){}
报第二种错误
String strAA ;
try {
strAA = "";
} catch(Exception e){}
System.out.println(strAA);
第二是因为关闭连接时必须要捕获异常,
即必须用try catch包起来
类变量会被自动初始化。
你的问题不是没有出错,是出错了,你没有捕捉。
-----------------------------------------------------------------------
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:db1","","");
.......................................................
}catch(Exception e){
System.out.println("create JdbcOdbcBridge error");
}finally {
try {
if(con != null) con.close();
}catch(Exception ee){}
}
你可以查看JAVA API DOCS关于Connection类close()方法的定义。
Connection con;
Statement sql;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:db1","","");
.......................................................
}catch(Exception e){
System.out.println("create JdbcOdbcBridge error");
}finally {
try {
if(con != null) con.close();
}catch(Exception ee){}
}
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
//假如这里出异常,con就是没有init!但是下面的try会继续执行!! }
catch(SQLException e)
{
System.out.println("creat connection error");
}
try
{
sql = con.createStatement();
//如果上面出异常,con可能是null
}
catch(SQLException e)
{
System.out.println("createStatement error");
}
}
}
--------------------------try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
//如果出异常,下面不执行了!
sql = con.createStatement();
con.close();
}
catch(SQLException e1)
{
System.out.println("error");
}
——————————————
try
{
con = DriverManager.getConnection("jdbc:odbc:db1","","");
}
catch(SQLException e)
{
System.out.println("creat connection error");
}
try
{
if(null!=con) sql = con.createStatement();
}
catch(SQLException e)
{
System.out.println("createStatement error");
}catch(Exception e2){
System.err.println(e2.toString());
}
try..catch就是一个局部作用域,严格来说所有被一对大括号封闭起来的部分都是局部作用域.在局部作用域里面做的操作,编译器编译的时候认为不能影响局部作用域之外的变量.
-------------------------------------------------------------------------------
我把你给的例子改了一下:
String strAA = "sss" ;
try {
strAA = "abc";
} catch(Exception e){}
try {
System.out.println(strAA);
} catch(Exception e){}
输出结果:abc
如果说 "在局部作用域里面做的操作,编译器编译的时候认为不能影响局部作用域之外的变量",那么这里strAA的值最终被输出应该是sss才对呀,因为对strAA的赋值语句和输出语句位于两个不同的try...case块
----------------------------------------------
再举一例:
class Student
{
String name;
void setName(String s)
{
name = s;
}
void echoName()
{
System.out.println(name);
]
}
public class BlockTest
{
public static void main(String args[])
{
Student st1 = new Student();
st1.setName("你的名字叫八戒");
st1.echoName;
]
}
编译以后运行输出结果: 你的名字叫八戒
如果说 "在局部作用域里面做的操作,编译器编译的时候认为不能影响局部作用域之外的变量",那么setName方法应该是局部作用域吧,那它里面做的操作应该不能影响局部作用域之外的变量--name,但是事实上Student类的数据成员name还是被setName方法赋值成为了"你的名字叫八戒",这是为什么
你看看这个代码:
String con=null;
try
{
throw new Exception("test");
con = "执行了";
//假如这里出异常,con就是没有init!但是下面的try会继续执行!! }
catch(Exception e)
{
System.out.println("error");
}
try
{
System.out.println(""+con.length());
}
catch(NullPointerException e)
{
System.out.println("我估计就是NullPonterException");
}
}
}
--------------------------
比较:
String con=null;
try
{
throw new Exception("test");
con = "执行了";//如果出异常,下面不执行了!
System.out.println(""+con.length());
//也不执行,不会有NullPonterException;
}
catch(NullPointerException e1)
{
System.out.println("error");
}
catch(Exception e1)
{
System.out.println("error");
}
请你注意,我说得是"java编译器编译的时候认为不能影响局部作用域之外的变量",而不是程序执行的时候.