Advertisement
Guest User

Untitled

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