gpt4 book ai didi

java - 无法解析连接变量

转载 作者:行者123 更新时间:2023-11-29 03:59:33 25 4
gpt4 key购买 nike

public class GestorBase{

public static void main(String[] args){
try
{
Class.forName("org.sqlite.JDBC");
}
catch (ClassNotFoundException e) {
System.out.println("Unable to load driver class");
// TODO: handle exception
}
try {
Connection con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("error al buscar la base de datos");
}

Statement sentencia = con.createStatement();


}}

eclipse 说:

"con" variable cannot be resolved to a type.

为什么?

最佳答案

con 变量是 try block 的局部变量,

try {
Connection con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
}

您正在 try block 之外访问 con

应该是

Connection con = null;
Statement sentencia = null;
try {
con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
sentencia = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("error al buscar la base de datos");
} catch (Exception ex){

//handle it
}

关于java - 无法解析连接变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com