Posted By

lasagna7355608 on 07/16/15


Tagged

databases


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

lasagna7355608


Connecting to a SQL Server in Java


 / Published in: Java
 

URL: basicdtabasesql

Basic Connection to a MS SQL Server in Java

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

Report this snippet  

You need to login to post a comment.