本文主要是介绍Java 连接Sql sever 2008,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java 连接Sql sever 2008 /Sql sever 2008 R2
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; public class TestJDBC { public static void main(String args[]){ String url = "jdbc:sqlserver://202.195.145.169:1368;databaseName=Student;user=sa;password=123456";//sa身份连接 Connection con = null; Statement stmt = null; ResultSet rs = null; try { 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 Login"; 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("Name") + " " + 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) { } } }
}
这篇关于Java 连接Sql sever 2008的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!