Last active
September 10, 2020 00:27
-
-
Save timothyshort/ba765e477a37ff119103492c05da298e to your computer and use it in GitHub Desktop.
JDBC: Connect to a database and read records (SQL Server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dependencies: sqljdbc4 JAR | |
// This method will connect to a SQL Server M.S. database and return a double array | |
public static String[][] get(String query) { | |
int numRows; | |
int numCols = 4; | |
String dataTable[][] = null; | |
// Define connection string | |
String connectionString; | |
/* | |
String server = ""; | |
String databaseName = ""; | |
String username = ""; | |
String password = ""; | |
*/ | |
connectionString = server + ";databasename=" + databaseName + ";username=" + username + ";password=" + password; | |
// Write SQL statement to query the databse (override) | |
// query = "select * from table"; | |
// Perform DB operations | |
try { | |
Connection oConnection = DriverManager.getConnection(connectionString); | |
Statement oStatement = oConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
ResultSet oResult = oStatement.executeQuery(query); | |
// Set the size of the array | |
oResult.last(); | |
numRows = oResult.getRow(); | |
dataTable = new String[numRows][numCols]; | |
// Read the DB file while there is a next element | |
oResult.beforeFirst(); | |
int row = 0; | |
while (oResult.next()) { | |
dataTable[row][0] = oResult.getString(1); | |
dataTable[row][1] = oResult.getString(2); | |
dataTable[row][2] = oResult.getString(3); | |
dataTable[row][3] = oResult.getString(4); | |
row++; | |
} | |
} catch (SQLException e) { | |
System.out.println("ERROR Handling SQL"); | |
e.printStackTrace(); | |
} | |
return dataTable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment