当前位置: 技术问答>java相关
why error???? help me
来源: 互联网 发布时间:2015-02-05
本文导语: import java.sql.*; public class db{ public static void main(String argv[]) { String url,user,pwd; url = "jdbc:odbc:test"; user = "sa"; pwd = ""; java.sql.ResultSet rs; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(Exception e) ...
import java.sql.*;
public class db{
public static void main(String argv[])
{
String url,user,pwd;
url = "jdbc:odbc:test";
user = "sa";
pwd = "";
java.sql.ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error1");
}
try
{
Connection con = DriverManager.getConnection(url,user,pwd);
}
catch(Exception e)
{
System.out.println("error2");
}
String sqlStr = "select * from bank";
String name;
PreparedStatement ps = con.prepareStatement(sqlStr);
rs = ps.executeQuery();
while(rs.next())
{
name = rs.getString("BANK_NM");
System.out.println(name);
}
}
}
提示不能解析con!
public class db{
public static void main(String argv[])
{
String url,user,pwd;
url = "jdbc:odbc:test";
user = "sa";
pwd = "";
java.sql.ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error1");
}
try
{
Connection con = DriverManager.getConnection(url,user,pwd);
}
catch(Exception e)
{
System.out.println("error2");
}
String sqlStr = "select * from bank";
String name;
PreparedStatement ps = con.prepareStatement(sqlStr);
rs = ps.executeQuery();
while(rs.next())
{
name = rs.getString("BANK_NM");
System.out.println(name);
}
}
}
提示不能解析con!
|
你把con放在try。。catch里面,出了之后,它的作用域也出去了
所以你后面就不能再用了
你要么把他们都放在你Connection con 里面的try里面一起
要么在外面先定义一下con对象
所以你后面就不能再用了
你要么把他们都放在你Connection con 里面的try里面一起
要么在外面先定义一下con对象
|
import java.sql.*;
public class db{
public static void main(String argv[])
{
Connection con ;
String url,user,pwd;
url = "jdbc:odbc:test";
user = "sa";
pwd = "";
java.sql.ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error1");
}
try
{
con= DriverManager.getConnection(url,user,pwd);
}
catch(Exception e)
{
System.out.println("error2");
}
String sqlStr = "select * from bank";
String name;
PreparedStatement ps = con.prepareStatement(sqlStr);
rs = ps.executeQuery();
while(rs.next())
{
name = rs.getString("BANK_NM");
System.out.println(name);
}
}
}
public class db{
public static void main(String argv[])
{
Connection con ;
String url,user,pwd;
url = "jdbc:odbc:test";
user = "sa";
pwd = "";
java.sql.ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error1");
}
try
{
con= DriverManager.getConnection(url,user,pwd);
}
catch(Exception e)
{
System.out.println("error2");
}
String sqlStr = "select * from bank";
String name;
PreparedStatement ps = con.prepareStatement(sqlStr);
rs = ps.executeQuery();
while(rs.next())
{
name = rs.getString("BANK_NM");
System.out.println(name);
}
}
}
|
变量的作用域问题
|
在
public static void main(String argv[])
{
下面定義
con
public static void main(String argv[])
{
下面定義
con