Advertisement
buzzonit

JAVA SQL PROJECT

Jul 20th, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. public class NewJFrame extends javax.swing.JFrame {
  2.  
  3. Connection con = null;
  4. ResultSet rs = null;
  5. PreparedStatement pst = null;
  6.  
  7.  
  8. https://www.youtube.com/watch?v=Z46OHxdEEEI
  9.  
  10. https://snag.gy/dFEeaZ.jpg part1
  11. https://snag.gy/sPYBy6.jpg part 2
  12.  
  13.  
  14.  
  15.  
  16. public static void viewTable(Connection con, String dbName)
  17. throws SQLException {
  18.  
  19. Statement stmt = null;
  20. String query = "select COF_NAME, SUP_ID, PRICE, " +
  21. "SALES, TOTAL " +
  22. "from " + dbName + ".COFFEES";
  23. try {
  24. stmt = con.createStatement();
  25. ResultSet rs = stmt.executeQuery(query);
  26. while (rs.next()) {
  27. String coffeeName = rs.getString("COF_NAME");
  28. int supplierID = rs.getInt("SUP_ID");
  29. float price = rs.getFloat("PRICE");
  30. int sales = rs.getInt("SALES");
  31. int total = rs.getInt("TOTAL");
  32. System.out.println(coffeeName + "\t" + supplierID +
  33. "\t" + price + "\t" + sales +
  34. "\t" + total);
  35. }
  36. } catch (SQLException e ) {
  37. JDBCTutorialUtilities.printSQLException(e);
  38. } finally {
  39. if (stmt != null) { stmt.close(); }
  40. }
  41. }
  42.  
  43.  
  44.  
  45.  
  46. //Exemplo com JDBC
  47. import java.sql.*;
  48. import javax.swing.JOptionPane;
  49. public class Conexao {
  50. public static void main(String args[]) {
  51. try {
  52. Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  53. Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://ivan:1433","sa","");
  54. Statement stmt = conn.createStatement();
  55. JOptionPane.showMessageDialog(null, "Conectou!");
  56. } catch(Exception e) {
  57. JOptionPane.showMessageDialog(null, e.toString());
  58. System.out.print(e.toString());
  59. }
  60. }
  61. }
  62.  
  63.  
  64.  
  65.  
  66. //Exemplo com JTDS
  67. import java.sql.*;
  68. import javax.swing.JOptionPane;
  69. public class Conexao {
  70. public static void main(String args[]) {
  71. try {
  72. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  73. Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://ivan:1433","sa","");
  74. Statement stmt = conn.createStatement();
  75. JOptionPane.showMessageDialog(null, "Conectou!");
  76. } catch(Exception e) {
  77. JOptionPane.showMessageDialog(null, e.toString());
  78. System.out.print(e.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement