Guest User

Untitled

a guest
Aug 3rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. Unable to get data from Sql Server 2005 (connection time out exception)
  2. jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
  3.  
  4. package edu.jtds.main;
  5.  
  6. import java.sql.*;
  7.  
  8. public class SqlServerConnTest {
  9.  
  10. Connection conn;
  11.  
  12. public void connect() {
  13. try {
  14. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  15. String dbName = "TestDB";
  16. String user = "cajeroUpz";
  17. String password = "cajero";
  18. //the name of my SQL SERVER 2005 instance
  19. String SqlServerInstance = "instance=SQL2005";
  20. String url = "jdbc:jtds:sqlserver://localhost:1433";
  21. url = url + "/" + dbName;
  22. url = url + ";" + SqlServerInstance;
  23. conn = DriverManager.getConnection(url, user, password);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public Connection getConnection() {
  30. return this.conn;
  31. }
  32.  
  33. public static void main(String[] args) {
  34. SqlServerConnTest oSqlServerConnTest = new SqlServerConnTest();
  35. oSqlServerConnTest.connect();
  36. String sql = "SELECT * FROM TEST_TABLE";
  37. Connection conn = null;
  38. Statement stat = null;
  39. ResultSet rs = null;
  40. try {
  41. conn = oSqlServerConnTest.getConnection();
  42. stat = conn.createStatement();
  43. rs = stat.executeQuery(sql);
  44. while(rs.next()) {
  45. System.out.println(String.format("%d %s",
  46. rs.getInt(1), rs.getString(2)));
  47. }
  48. rs.close();
  49. stat.close();
  50. conn.close();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56.  
  57. 1 hello world
  58. 2 goodbye!
Add Comment
Please, Sign In to add comment