Connect to SQL


SUBMITTED BY: knightley

DATE: Sept. 3, 2015, 9:25 a.m.

FORMAT: Java

SIZE: 4.0 kB

HITS: 601

  1. import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
  2. import java.sql.*;
  3. public void connectToDB() {
  4. try {
  5. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  6. } catch (Exception e) {
  7. System.out.printf(" Unable to load JDBC driver... '%s'\n", e.getMessage());
  8. return;
  9. }
  10. if (true) {
  11. try {
  12. String connectionString = "jdbc:sqlserver://OPENBOX\\WRR;databaseName=WRAP301";
  13. System.out.println(" Connection string = " + connectionString);
  14. con = DriverManager.getConnection(connectionString, "WRAP301User", "1");
  15. stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  16. } catch (Exception e) {
  17. System.out.printf(" Unable to connect to DB... '%s'\n", e.getMessage());
  18. }
  19. }
  20. public void useDB() {
  21. try {
  22. // perform query on database and retrieve results
  23. String sql = "SELECT * FROM TableName";
  24. System.out.println(" Performing query, sql = " + sql);
  25. ResultSet result = stmt.executeQuery(sql);
  26. System.out.println(" Displaying Query Result");
  27. while (result.next()) {
  28. String surname = result.getString("surname");
  29. String name = result.getString("name");
  30. String phone = result.getString("phone");
  31. String cell = result.getString("cell");
  32. System.out.println(" " + surname + ", " + name + ", (p) " + phone + ", (c) " + cell);
  33. }
  34. result.close();
  35. } catch (Exception e) {
  36. System.out.println(" Was not able to query database...");
  37. }
  38. }
  39. public void addRecord() {
  40. try {
  41. String sql = "INSERT INTO Person VALUES ('Somesurnameelse', 'Aname', '1234', '5678')";
  42. stmt.execute(sql);
  43. System.out.println("\tDone!");
  44. } catch (Exception e) {
  45. System.out.println("Could not insert new record... " + e.getMessage());
  46. }
  47. }
  48. public void getMetaData() {
  49. try {
  50. // perform query on database and retrieve results
  51. String sql = "SELECT * FROM TableName";
  52. System.out.println(" Performing query, sql = " + sql);
  53. ResultSet result = stmt.executeQuery(sql);
  54. ResultSetMetaData meta = result.getMetaData();
  55. int columns = meta.getColumnCount();
  56. System.out.println("\tColumns = " + columns);
  57. for (int i = 1; i <= columns; i++) {
  58. String colName = meta.getColumnLabel(i);
  59. String colType = meta.getColumnTypeName(i);
  60. System.out.println("\tcol[" + i + "]: name = " + colName + ", type = " + colType);
  61. }
  62. int row = 0;
  63. while (result.next()) {
  64. // get values from current tuple
  65. row++;
  66. String line = "\tRow[" + row + "]=";
  67. for (int i = 1; i <= columns; i++) {
  68. line = line.concat(result.getString(i) + " ");
  69. }
  70. }
  71. } catch (Exception e) {
  72. System.out.println("Could not query database... " + e.getMessage());
  73. }
  74. }
  75. public void disconnectDB() {
  76. try {
  77. con.close();
  78. } catch (Exception ex) {
  79. System.out.println(" Unable to disconnect from database");
  80. }
  81. }
  82. }

comments powered by Disqus