Java: code to verify Java can connect to 1995 SQL Server Database
This file contains 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
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment