Я исчерпал все возможности, чтобы разобраться в этой проблеме, но безрезультатно. Я установил «SQL Management Studio 2012» и создал фиктивную базу данных и методы, но я все еще получаю «указатель исключения нулевой точки». Java и JDBC установлены под переменными пользователя.Подключение Java к базе данных
Вот скриншоты и код.
Static {
// standard code to open a connection and statement to SQL Server database
try {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://SQL-SERVER;"
+ "databaseName=ItunesDB;integratedSecurity=true;";
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
} // Handle any errors that may have occurred.
catch (SQLException sqle) {
System.out.println("Sql Exception :" + sqle.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("Class Not Found Exception :" + e.getMessage());
}
}
public static String listAll() {
String output = "";
try {
stmt = con.createStatement();
ResultSet res = stmt.executeQuery("SELECT * FROM LibraryTable");
while (res.next()) { // there is a result
// the name field is the thrid one in the ResultSet
// Note that with ResultSet we count the fields starting from 1
output += res.getString(1) + " " + res.getString(2) + " - "
+ res.getString(3) + " " + res.getString(4) + " "
+ res.getString(5) + "\n";
}
} catch (Exception e) {
System.out.println(e);
return null;
}
return output;
}
public static String getName(String key) {
try {
SELECT * FROM LibraryTable WHERE key = '04'
stmt = con.createStatement();
ResultSet res = stmt.executeQuery("SELECT * FROM LibraryTable WHERE ID = '" + key + "'");
if (res.next()) { // there is a result
// the name field is the second one in the ResultSet
// Note that with ResultSet we count the fields starting from 1
return res.getString(2);
} else {
return null;
}
} catch (Exception e) {
System.out.println(e);
return null;
}`enter code here`
Информационная база:
Dummy Database
ID Name Artist Quantity Price Rating Playcount
Что мне нужно сделать, чтобы исправить это?
где и получить исключения нулевого указателя ?? – Kick
Где находится NPE? Трассировка стека была бы полезна. – Makoto
Момент, который еще один класс в моей программе пытается проверить списки треков. Очевидно, он работал перед добавлением кода SQL в мой класс библиотеки. – user3305841