1.需要jar包sqljdbc.jar(附件提供),下载地址
[url]http://www.microsoft.com/downloads/details.aspx?FamilyID=e22bc83b-32ff-4474-a[/url]44a-22b6ae2c4e17
2.SqlServer的tcp/ip端口查询
SQL Server Configuration Manager-SQL Server 2005 网络配置-SQLEXPRESS-右键单击ICP/IP-属性-IP地址
3.设置sa账户与密码
使用Microsoft SQL Server Management Studio Express管理SqlServer数据库,第一次选择Windows身份验证模式登录。
3.1右键sqlserver-安全性-服务器身份验证-“SQL SERVER和Windows身份验证模式”;
3.2安全性-登录名-双击sa,设置用户名和密码;
3.3在sa的右侧-状态-登录-选择启用;
4.建立测试数据库并导入sql脚本
4.1数据库-右键新建数据库-tmp;
4.2导入下面的sql脚本,选择执行。
student.sql
use tmp;
CREATE TABLE student_table (
id varchar(32) NOT NULL,
name varchar(32) DEFAULT NULL,
cardId varchar(32) NOT NULL,
age int DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO student_table VALUES
('297ed8ec1efde79a011efde79e670001','tomclus','200512345',33);
5.Java连接文件
SqlServerCon.java
<b>package</b> com.zj.connector;
<b>import</b> java.sql.Connection;
<b>import</b> java.sql.DriverManager;
<b>import</b> java.sql.ResultSet;
<b>import</b> java.sql.Statement;
<b>public</b> <b>class</b> SqlServerCon {
<b>public</b> <b>static</b> <b>void</b> main(String[] arg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1025; DatabaseName=tmp";// 连接服务器和数据库
String userName = "sa"; // 用户名
String userPwd = "root"; // 密码
Connection dbConn;
<b>try</b> {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement stmt = dbConn.createStatement();
ResultSet rs = <b>null</b>;
String sql = "select * from student_table";
rs = stmt.executeQuery(sql);
<b>while</b> (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String cardId = rs.getString("cardId");
<b>int</b> age = rs.getInt("age");
System.out.println(id + " " + name + " " + cardId + " " + age);
}
} <b>catch</b> (Exception e) {
e.printStackTrace();
}
}
}
MySQLCon.java
<b>public</b> <b>class</b> MySQLCon {
String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动
String dbURL = "jdbc:mysql://localhost:3306/tmp?useUnicode=true&amp;characterEncoding=UTF-8"; // 连接服务器和数据库
String userName = "root"; // 用户名
Connection dbConn;
本文转自zhangjunhd51CTO博客,原文链接:http://blog.51cto.com/zhangjunhd/127849,如需转载请自行联系原作者