Advertisement
Guest User

Untitled

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