Java-在创建连接并返回连接的函数中找不到符号错误
我正在尝试将Java应用程序连接到数据库,我遇到以下错误,我不知道它们是什么意思,我是Java开发新手,因此如果有人能帮助我,我将不胜感激:Java-在创建连接并返回连接的函数中找不到符号错误,java,netbeans,Java,Netbeans,我正在尝试将Java应用程序连接到数据库,我遇到以下错误,我不知道它们是什么意思,我是Java开发新手,因此如果有人能帮助我,我将不胜感激: symbol: variable SEVERE location: class Level Main.java:44: error: cannot find symbol Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
symbol: variable SEVERE
location: class Level
Main.java:44: error: cannot find symbol
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
^
symbol: method getLogger(String)
location: interface Logger
除此之外,IDE建议我做以下几点:
connection = (Connection) datasource.getConnection();
而不是:
connection = datasource.getConnection();
我的代码:
package My_Classes;
import com.mysql.cj.jdbc.MysqlDataSource;
import com.sun.jdi.connect.spi.Connection;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.sql.SQLException;
/**
*
* @author aldin
*/
public class DB {
//Pravimo konekciju izmedju projekta i mysql-a
private static String username = "root";
private static String servername = "localhost";
private static String dbname = "java_library_system";
private static Integer portNumber = 3306;
private static String password = ""; //nema sifre
//funkcija za konekciju
public static Connection getConnection() throws SQLException
{
Connection connection = null;
MysqlDataSource datasource = new MysqlDataSource();
datasource.setServerName(servername);
datasource.setUser(username);
datasource.setDatabaseName(dbname);
datasource.setPortNumber(portNumber);
datasource.setPassword(password);
try{
connection = datasource.getConnection();
}catch(SQLException ex){
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
return connection;
}
}
有人能告诉我我做错了什么吗?所以,我在添加库时出错了。我只添加了IDE建议我添加的内容,而必需的内容是:
import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
检查
import
语句(例如com.sun.jdi.connect.spi.Connection
可能是错误的,java.lang.System.Logger
也很可疑,更像java.sql.Connection
和java.util.logging.Logger
)我删除了所有库,并从您的建议中包含了一些库,现在我只有以下内容:import com.mysql.cj.jdbc.MysqlDataSource;导入java.lang.System.Logger.Level;导入java.sql.Connection;导入java.util.logging.Logger;导入java.sql.SQLException代码>但获取此错误: