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