I want to be able to call a stored procedure from a Microsoft SQL Server database and list the results to the console (that is to start with). At someone I want to put all that data either to a JTable or to the web but first I will be happy if I can display it to the Console.
I think that the problem is that I cannot (or probably do not know how) to establish a connection to the database.
How can I connect to my Microsoft SQL Server database? (I have seen it done before and I think that it was 4 lines of code. I want this to work on my machine so I can start testing out some of the stored procedure.
Java Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLServerTest {
private ResultSet[] yourResult;
private void showSuppliers(ResultSet[] rs) throws SQLException {
Connection con =
DriverManager.getConnection("jdbc:default:connection");
Statement stmt = null;
String query = "select * from PeopleOne";
stmt = con.createStatement();
rs[0] = stmt.executeQuery(query);
}
private void ShowResultsToConsole(ResultSet[] rs) throws SQLException {
for (ResultSet r : rs) {
int c = 0;
r.getString(c);
}
}
public static void main(String[] p) throws SQLException {
SQLServerTest temp = new SQLServerTest();
temp.showSuppliers(temp.yourResult);
temp.ShowResultsToConsole(temp.yourResult);
}
}