public
Last active

Java: code to verify Java can connect to 1995 SQL Server Database

  • Download Gist
OldSchoolJDBC.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package old.school;
 
import java.sql.*;
 
/**
* run as follows:
* 1. javac OldSchoolJDBC.java
* 2. java OldSchoolJDBC
*/
public class OldSchoolJDBC {
Connection conn;
 
public OldSchoolJDBC() {}
 
public void connect() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:windows-datasource-name",
"USERID", "PASSWORD");
}
 
public void query() throws Exception {
Statement stmt = conn.createStatement();
String qry = "SELECT * FROM sc_admin.abendcodesa4";
ResultSet rs = stmt.executeQuery(qry);
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
 
public static void main(String[] args) {
try {
OldSchoolJDBC p = new OldSchoolJDBC();
p.connect();
p.query();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.