Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Display SQL Server table values using jdbc
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class DateServer{
- public static void main(String[] argv) throws Exception {
- String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- String user = "abc";
- String pass = "def";
- Class.forName(driver).newInstance();
- Connection con = DriverManager.getConnection("jdbc:sqlserver://<hostname:port;database name>, user, pass);
- System.out.println("connected");
- Statement st = con.createStatement();
- ResultSet res = st.executeQuery("SELECT CommentID FROM Comment");
- while (res.next()) {
- int i = res.getInt("CommentID");
- System.out.println(i);
- }
- con.close();
- }
- }
- Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'Comment'.
- at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
- at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
- at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
- at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:611)
- at DateServer.main(DateServer.java:19)
- st.executeQuery("SELECT CommentID FROM "Comment"");
Add Comment
Please, Sign In to add comment