Java JDBC------------------ODBC (SQL Server) links

Source: Internet
Author: User
Tags odbc connection


Java JDBC------------------ODBC (SQL Server) links


JDBC is a standard set of APIs that Java provides for connecting databases, developed in the traditional Jdbc-odbc bridge. Today I will write


How to link a SQL Server database with JDBC configuration data source (ODBC).



1. Configure the data source


Start, Control Panel, administrative tools, data sources, select SQL Server, note that starting SQL Server services


To ensure that the service TCP/IP protocol starts and the port number is 1433


2. Write Database code

package java_data_jdbc_sqlserver;import java.sql.connection;import java.sql.drivermanager; import java.sql.resultset;import java.sql.statement;/* *  testing ODBC connection SQL Server database  *  1. Configure Data source  * 2, connect database  * 3, query data  */public class Data_query {private  static string url =  "Jdbc:odbc:Hello";p rivate static string user =   "sa";p rivate static string password =  "12345678";p rivate static  string sql =  "Select * from t_student";p ublic static void main ( String[] args)  {try {//Load Database driver Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");// Get connection to database Connection conn = drivermanager.getconnection ( url , user ,  password);//Get Statement Object Statement stat = conn.createstatement ();//query data, get result set Resultset rs  = stat.executequery (SQL);//Get result set, take out data wHile (Rs.next ()) {string stuno = rs.getstring ("Stuno"); String stuname = rs.getstring ("Stuname"); String stusex = rs.getstring ("Stusex"); System.out.println ("School Number:" +stuno +  "   Name:"  + stuName +  "   Gender:"  + stusex);} Close result set Conn.close (); Stat.close ();}  catch  (exception e)  {e.printstacktrace ();}}


Note that the Sun.jdbc.odbc.JdbcOdbcDriver class is removed from Java JDK8, so the above program must be in JDK7 or the following version


This operation.


Also, today the Java official website released the JAVA9, this technology update is too fast! Tell me why I love you!

This article is from the "@coder" blog, be sure to keep this source http://smallcoder.blog.51cto.com/11941149/1855635

Java JDBC------------------ODBC (SQL Server) links

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.