標籤:blog http java os 資料 io for 2014
package xhs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyStringClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動產生的方法存根
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=AdventureWorks;integratedSecurity=true;";
String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=QQhrlgzysyWeb;user=sa;password=123";//sa身份串連
//String url2 = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydbdemo;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 TOP 10 * FROM NewsInfo";
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(4) + " " + rs.getString(6));
}
}
// 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) {
}
}
}
}
sa身分識別驗證模式
sqlserver2008用的是動態連接埠,需要你配置一下:
開啟組態工具->SQLServer組態管理員->SQLServer網路設定->MSSQLSERVER的協議->TCP/IP啟用,把TCP動態連接埠中的0都刪掉,留空;然後把列表拉到最下邊(IPALL),配置一個固定連接埠,以後你串連資料庫就用這個連接埠就可以了:如