当前位置:首页 > 数据库 > Sqlserver

DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程

java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; /** * Created by crazyacking on 2015/6/27. */ public class DBLinkTestMain { public static void main(String args[]) { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=DBLinkTest;integratedSecurity=true;"; String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=DBLinkTest;user=crazyacking;password=YOURPASSWORD";//sa身份连接 String url2 = "jdbc:sqlserver://127.0.0.1:1433;databaseName=DBLinkTest;integratedSecurity=true;";//windows集成模式连接 // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Establish the connection. System.out.println("begin."); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(url); System.out.println("end."); // Create and execute an SQL statement that returns some data. String SQL = "SELECT * FROM TestTable1"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println(rs.getString(1) + " " + rs.getString(2)); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch (Exception e) { } if (stmt != null) try { stmt.close(); } catch (Exception e) { } if (con != null) try { con.close(); } catch (Exception e) { } } } }

 

DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程

标签:

本文系统来源:http://www.cnblogs.com/crazyacking/p/4604684.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!