出错信息
Hit uncaught exception java.lang.NullPointerException
error creating connection:
Exception in thread "main" java.lang.NullPointerException
at oracledatabase.DatabaseJdbc.getResultsByColumnName
at oracledatabase.DatabaseJdbc.main
代码如下:
package oracledatabase;import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSetMetaData;public class DatabaseJdbc
{
public static void main(String[] args) {
DatabaseJdbc databasejdbc = new DatabaseJdbc();
databasejdbc.getResultsByColumnName();
databasejdbc.getResultsByColumnPosition();
databasejdbc.getAllColumn();
databasejdbc.closeConnection();
}
public DatabaseJdbc(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (SQLException sqle) {
System.err.println("error creating connection:");
} catch (ClassNotFoundException ex) {
/** @todo Handle this exception */
System.err.println(ex.toString());
}
}
void getResultsByColumnName() {
try {
ResultSet resultSet = statement.executeQuery(allName);
int row = 0;
while(resultSet.next()) {
System.out.println("Row " + (++row) + ") " +
resultSet.getString("name") + " " +
resultSet.getString("id"));
}
resultSet.close();
} catch (SQLException sqle) {
System.out.println("\nSQLException: \n--- ");
System.out.println("SQL state: " + sqle.getSQLState());
// System.out.println("Exception deal: " + sqle.getNextException());
System.out.println("Message: " + sqle.getMessage()); }
}
void getResultsByColumnPosition() {
try {
ResultSet resultSet = statement.executeQuery(allName);
int row = 0;
while(resultSet.next()) {
System.out.println("\nRow " + (++row) + ")");
for(int i=0; i<=2;i++){
System.out.println((i>1?", ":" ") + resultSet.getString(i));
}
}
resultSet.close();
} catch (SQLException sqle) {
System.out.println("SQLException: \n--- ");
System.out.println("SQL state: " + sqle.getSQLState());
// System.out.println("Exception deal: " + sqle.getNextException());
System.out.println("Message: " + sqle.getMessage());
}
}
void getAllColumn() {
try {
ResultSet resultSet = statement.executeQuery(allName);
ResultSetMetaData metadata = resultSet.getMetaData();
// Column count
int column = metadata.getColumnCount();
int row = 0;
while(resultSet.next()) {
System.out.println("\nRow " + (++row) + ")");
for(int i=0; i<=2;i++){
System.out.println((i>1?", ":" ") + resultSet.getString(i));
}
}
resultSet.close();
} catch (Exception ex) { }
}
void closeConnection() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException ex) {
System.out.println("SQLException: \n--- ");
System.out.println("SQL state: " + ex.getSQLState());
// System.out.println("Exception deal: " + ex.getNextException());
System.out.println("Message: " + ex.getMessage());
}
}
}
Connection connection;
Statement statement;
String sourceURL = "jdbc:oracle:thin:@localhost:1521:system";
String userNameAndID = "select name,id from SYS_WORKER";
String allName = "select * from SYS_WORKER";
}
Hit uncaught exception java.lang.NullPointerException
error creating connection:
Exception in thread "main" java.lang.NullPointerException
at oracledatabase.DatabaseJdbc.getResultsByColumnName
at oracledatabase.DatabaseJdbc.main
代码如下:
package oracledatabase;import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSetMetaData;public class DatabaseJdbc
{
public static void main(String[] args) {
DatabaseJdbc databasejdbc = new DatabaseJdbc();
databasejdbc.getResultsByColumnName();
databasejdbc.getResultsByColumnPosition();
databasejdbc.getAllColumn();
databasejdbc.closeConnection();
}
public DatabaseJdbc(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (SQLException sqle) {
System.err.println("error creating connection:");
} catch (ClassNotFoundException ex) {
/** @todo Handle this exception */
System.err.println(ex.toString());
}
}
void getResultsByColumnName() {
try {
ResultSet resultSet = statement.executeQuery(allName);
int row = 0;
while(resultSet.next()) {
System.out.println("Row " + (++row) + ") " +
resultSet.getString("name") + " " +
resultSet.getString("id"));
}
resultSet.close();
} catch (SQLException sqle) {
System.out.println("\nSQLException: \n--- ");
System.out.println("SQL state: " + sqle.getSQLState());
// System.out.println("Exception deal: " + sqle.getNextException());
System.out.println("Message: " + sqle.getMessage()); }
}
void getResultsByColumnPosition() {
try {
ResultSet resultSet = statement.executeQuery(allName);
int row = 0;
while(resultSet.next()) {
System.out.println("\nRow " + (++row) + ")");
for(int i=0; i<=2;i++){
System.out.println((i>1?", ":" ") + resultSet.getString(i));
}
}
resultSet.close();
} catch (SQLException sqle) {
System.out.println("SQLException: \n--- ");
System.out.println("SQL state: " + sqle.getSQLState());
// System.out.println("Exception deal: " + sqle.getNextException());
System.out.println("Message: " + sqle.getMessage());
}
}
void getAllColumn() {
try {
ResultSet resultSet = statement.executeQuery(allName);
ResultSetMetaData metadata = resultSet.getMetaData();
// Column count
int column = metadata.getColumnCount();
int row = 0;
while(resultSet.next()) {
System.out.println("\nRow " + (++row) + ")");
for(int i=0; i<=2;i++){
System.out.println((i>1?", ":" ") + resultSet.getString(i));
}
}
resultSet.close();
} catch (Exception ex) { }
}
void closeConnection() {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException ex) {
System.out.println("SQLException: \n--- ");
System.out.println("SQL state: " + ex.getSQLState());
// System.out.println("Exception deal: " + ex.getNextException());
System.out.println("Message: " + ex.getMessage());
}
}
}
Connection connection;
Statement statement;
String sourceURL = "jdbc:oracle:thin:@localhost:1521:system";
String userNameAndID = "select name,id from SYS_WORKER";
String allName = "select * from SYS_WORKER";
}
===========
这个可能是null,你检查一下
谢谢了
你是通过错误信息,得到是connection是null,还是?