码迷,mamicode.com
首页 > 数据库 > 详细

JDBC连接SQLSERVER

时间:2014-07-29 14:00:08      阅读:309      评论:0      收藏:0      [点我收藏+]

标签: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),配置一个固定端口,以后你连接数据库就用这个端口就可以了:如下图

bubuko.com,布布扣

JDBC连接SQLSERVER,布布扣,bubuko.com

JDBC连接SQLSERVER

标签:blog   http   java   os   数据   io   for   2014   

原文地址:http://www.cnblogs.com/xrtd/p/3875218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!