编程论坛's Archiver

wymljl 发表于 2007-4-4 01:01

这个问题

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

欢天喜地 发表于 2007-8-7 22:17

你的监听服务没有启动把?没有服务是不行的,而且我看你现在还在使用vector,这个东西已经过时了,你可以选择别的用一下.

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.