import java.sql.*;import javax.swing.*; class jdbcExample1{ public static void main(String args[]){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:studyDSN"; String login="sa"; String password=""; Connection conn=DriverManager.getConnection(url,login,password); Statement statement=conn.createStatement(); String sqlQuery="select * from course"; ResultSet rs=statement.executeQuery(sqlQuery); while(rs.next()){ System.out.print(rs.getString(1)+","); System.out.print(rs.getString(2)+","); System.out.print(rs.getInt(3)+","); } statement.close(); conn.close(); } catch(SQLException sqlException){ JOptionPane.showMessageDialog(null,sqlException.getMessage(),"database error",JOptionPane.ERROR_MESSAGE); System.exit(1); } catch(ClassNotFoundException classNotFound){ JOptionPane.showMessageDialog(null,classNotFound.getMessage(),"driver not found",JOptionPane.ERROR_MESSAGE); System.exit(1); } }}

评论