Unable to connect
java.sql.SQLException: [Oracle][ODBC][Ora]ORA-12541: TNS: 没有监听器
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TableDisplay.<init>(TableDisplay.java:17)
at TableDisplay.main(TableDisplay.java:114)
Exception in thread "main" java.lang.NullPointerException
at TableDisplay.getTable(TableDisplay.java:38)
at TableDisplay.<init>(TableDisplay.java:27)
at TableDisplay.main(TableDisplay.java:114)
这个问题怎么解决?
我的java代码如下:
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class TableDisplay extends JFrame {
private Connection connection;
private JTable table;
public TableDisplay(){
String url = "jdbc:odbc:mouse";
String username = "system";
String password = "manager";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
System.out.println("Failed to load JDBC/ODBC driver");
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
System.err.println("Unable to connect");
e.printStackTrace();
}
getTable();
setSize(450,150);
setVisible(true);
}
private void getTable(){
Statement statement;
ResultSet resultSet;
String query = "select * from students";
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
displayResultSet(resultSet);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void displayResultSet(ResultSet rs){
boolean moreRecords = false;
try {
moreRecords = rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
if(!moreRecords){
JOptionPane.showMessageDialog(this,"ResultSet contained no records");
setTitle("No records to display");
return;
}
setTitle("Students table from mouse");
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData();
for(int i = 1;i<=rsmd.getColumnCount();++i)
columnHeads.addElement(rsmd.getCatalogName(i));
do{
rows.addElement(getNextRow(rs,rsmd));
}while(rs.next());
table = new JTable(rows,columnHeads);
JScrollPane scroller = new JScrollPane(table);
getContentPane().add(scroller,BorderLayout.CENTER);
this.validate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private Vector getNextRow(ResultSet rs,ResultSetMetaData rsmd){
Vector currentRow = new Vector();
try {
for(int i=1;i<=rsmd.getColumnCount();++i)
switch(rsmd.getColumnType(i)){
case Types.VARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
System.out.println("Type was :"+rsmd.getColumnTypeName(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
return currentRow;
}
public void shutDown(){
try {
connection.close();
} catch (SQLException e) {
System.out.println("Unable to disconnect");
e.printStackTrace();
}
}
public static void main(String[] args) {
TableDisplay td = new TableDisplay();
td.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//td.shutDown();
System.exit(0);
}
});
}
}