Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. package testing_sql_server;
  2.  
  3. import java.sql.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.util.*;
  8.  
  9. public class DisplayQuery1 extends JFrame { //CHANGE THIS!!
  10. private Connection connection;
  11. private JTable table;
  12.  
  13. public DisplayQuery1() //CHANGE THIS!!
  14. {
  15. // The URL specifying the 675Examples database to which
  16. // this program connects using JDBC to connect to a
  17. // Microsoft ODBC database.
  18. //String url = "jdbc:odbc:675Examples"; //CHANGE THIS!
  19. String url = "jdbc:odbc:SQLEXPRESS2008EXAMPLES";
  20. String username = "sa";
  21. String password = "testing123";
  22.  
  23. // Load the driver to allow connection to the database
  24. try {
  25. Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
  26.  
  27. connection = DriverManager.getConnection(
  28. url, username, password );
  29. }
  30. catch ( ClassNotFoundException cnfex ) {
  31. System.err.println(
  32. "Failed to load JDBC/ODBC driver." );
  33. cnfex.printStackTrace();
  34. System.exit( 1 ); // terminate program
  35. }
  36. catch ( SQLException sqlex ) {
  37. System.err.println( "Unable to connect" );
  38. sqlex.printStackTrace();
  39. }
  40.  
  41. getTable();
  42.  
  43. setSize( 450, 150 );
  44. show();
  45. }
  46.  
  47. private void getTable()
  48. {
  49. Statement statement;
  50. ResultSet resultSet;
  51.  
  52. try {
  53. String query = "select sr.name, sr.phone from Sales_Rep sr, Car c where sr.name = c.seller and c.color = 'green';"; //CHANGE THIS!
  54.  
  55. statement = connection.createStatement();
  56. resultSet = statement.executeQuery( query );
  57. displayResultSet( resultSet );
  58. statement.close();
  59. }
  60. catch ( SQLException sqlex ) {
  61. sqlex.printStackTrace();
  62. }
  63. }
  64.  
  65. private void displayResultSet( ResultSet rs )
  66. throws SQLException
  67. {
  68. // position to first record
  69. boolean moreRecords = rs.next();
  70.  
  71. // If there are no records, display a message
  72. if ( ! moreRecords ) {
  73. JOptionPane.showMessageDialog( this,
  74. "ResultSet contained no records" );
  75. setTitle( "No records to display" );
  76. return;
  77. }
  78.  
  79. setTitle( "Car Table from SQL HomeWork" ); //CHANGE THIS!!
  80.  
  81. Vector columnHeads = new Vector();
  82. Vector rows = new Vector();
  83.  
  84. try {
  85. // get column heads
  86. ResultSetMetaData rsmd = rs.getMetaData();
  87.  
  88. for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
  89. columnHeads.addElement( rsmd.getColumnName( i ) );
  90.  
  91. // get row data
  92. do {
  93. rows.addElement( getNextRow( rs, rsmd ) );
  94. } while ( rs.next() );
  95.  
  96. // display table with ResultSet contents
  97. table = new JTable( rows, columnHeads );
  98. JScrollPane scroller = new JScrollPane( table );
  99. getContentPane().add(
  100. scroller, BorderLayout.CENTER );
  101. validate();
  102. }
  103. catch ( SQLException sqlex ) {
  104. sqlex.printStackTrace();
  105. }
  106. }
  107.  
  108. private Vector getNextRow( ResultSet rs,
  109. ResultSetMetaData rsmd )
  110. throws SQLException
  111. {
  112. Vector currentRow = new Vector();
  113.  
  114. for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
  115. switch( rsmd.getColumnType( i ) ) {
  116. case Types.VARCHAR:
  117. currentRow.addElement( rs.getString( i ) );
  118. break;
  119. case Types.INTEGER:
  120. currentRow.addElement(
  121. new Long( rs.getLong( i ) ) );
  122. break;
  123. default:
  124. System.out.println( "Type was: " +
  125. rsmd.getColumnTypeName( i ) );
  126. }
  127.  
  128. return currentRow;
  129. }
  130.  
  131. public void shutDown()
  132. {
  133. try {
  134. connection.close();
  135. }
  136. catch ( SQLException sqlex ) {
  137. System.err.println( "Unable to disconnect" );
  138. sqlex.printStackTrace();
  139. }
  140. }
  141.  
  142. public static void main( String args[] )
  143. {
  144. final DisplayQuery1 app = new DisplayQuery1(); //CHANGE THIS!
  145.  
  146. app.addWindowListener(new WindowAdapter() {
  147. public void windowClosing( WindowEvent e )
  148. {
  149. app.shutDown();
  150. System.exit( 0 );
  151. }
  152. }
  153. );
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement