标题:这个问题
只看楼主
wymljl
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-11-27
 问题点数:0 回复次数:1 
这个问题

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);
}
});

}

}

搜索更多相关主题的帖子: Source odbc Unknown jdbc sun 
2007-04-04 01:01
欢天喜地
Rank: 2
等 级:论坛游民
威 望:2
帖 子:83
专家分:20
注 册:2007-5-13
得分:0 
你的监听服务没有启动把?没有服务是不行的,而且我看你现在还在使用vector,这个东西已经过时了,你可以选择别的用一下.
2007-08-07 22:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-129052-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.059186 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved