Guest User

Untitled

a guest
Aug 14th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. Display SQL Server table values using jdbc
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6.  
  7.  
  8. public class DateServer{
  9. public static void main(String[] argv) throws Exception {
  10. String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  11. String user = "abc";
  12. String pass = "def";
  13.  
  14. Class.forName(driver).newInstance();
  15. Connection con = DriverManager.getConnection("jdbc:sqlserver://<hostname:port;database name>, user, pass);
  16.  
  17. System.out.println("connected");
  18.  
  19. Statement st = con.createStatement();
  20. ResultSet res = st.executeQuery("SELECT CommentID FROM Comment");
  21. while (res.next()) {
  22. int i = res.getInt("CommentID");
  23. System.out.println(i);
  24. }
  25. con.close();
  26. }
  27. }
  28.  
  29. Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'Comment'.
  30. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
  31. at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
  32. at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
  33. at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
  34. at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
  35. at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
  36. at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
  37. at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
  38. at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
  39. at DateServer.main(DateServer.java:19)
  40.  
  41. st.executeQuery("SELECT CommentID FROM "Comment"");
Add Comment
Please, Sign In to add comment