Java access to SQL Server databases

Source: Internet
Author: User
Tags odbc

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.