Control Panel-management tool-ODBC data source (64-bit)-System dns-Add (name "Test", Server Fill ".") Description Optional)
The database accessed here is Adventuerworks
The data source is configured so you can test it, and here's the Java code:
Public static void Main (String args[]) {Connection con;//link java.sql.Statement sql;//statement resultset rs;//Query result try { Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");//Establish Bridge connection} catch (ClassNotFoundException e) {System.out.println ("" +e);} try {con = drivermanager.getconnection ("Jdbc:odbc:test", "" "," ");//Establish Connection object (data source name test, user name and password not) sql = Con.createstatement ();//Build Query Statement Object rs = sql.executequery ("SELECT * from Account");//Execute SQL statement while (Rs.next ()) { String name = Rs.getstring (1);//Output first column data string number = Rs.getstring (2);//Output second column data string Age = rs.getstring (3) ;//Output The third column of data System.out.println (name+ " " +number+ "" +age);//Output database query result} con.close ();//Close database connection} catch ( SQLException e) {System.out.println (e); E.printstacktrace ();}}}
Access to the Account data table in AdventureWorks
Results of the Java query
Results of SQL Server queries
Java access to the SQL Server database