1.在eclipse中导入sqljdbc4.jar
(参考:http://blog.csdn.net/mazhaojuan/article/details/21403717)
(sqljdbc.jar 和sqljdbc_auth.dll,可从这里下载:http://download.csdn.net/detail/outofmemo/9712646)
import com.microsoft.sqlserver.jdbc.SQLServerDriver.*
2.运行到"Class.forName"时出错,提示找不到类:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
,(实测将sqljdbc4.jar添加到CLASSPATH不起作用),把sqljdbc4.jar复制到tomcat的lib目录(D:\Program Files\MyEclipse Professional 2014\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_11.0.0.me201211151802\tomcat\lib)下可运行.
3.,java.library.path中找不到sqljdbc_auth.dll:
Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
,(亲测将其复制到system32无效)
使用 System.out.println(System.getProperty("java.library.path")); 查看java.library.path:
D:\Program Files\MyEclipse Professional 2014\binary\com.sun.java.jdk7.win32.x86_64_1.7.0.u45\bin;D:\Program Files\MyEclipse Professional 2014\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_11.0.0.me201211151802\tomcat\bin
将sqljdbc_auth.dll复制到其中一个目录
///////////////////////////////////////////////////////////////////////////////////////////
以下摘自网路:
若要连接到远程服务器上的默认实例,请使用:
String url = "jdbc:sqlserver://MyServer:integratedSecurity=true;"
若要连接到服务器上的特定端口,请使用:
String url = "jdbc:sqlserver://Myserver:1533;integratedSecurity=true;"
若要连接到服务器上的指定实例,请使用:
String url = "jdbc:sqlserver://MyServer;instanceName=INSTANCE1;integratedSecurity=true;"
若要连接到服务器上的特定数据库,请使用:
String url = "jdbc:sqlserver://MyServer;database=AdventureWorks;integratedSecurity=true;"
import java.sql.*;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class sqlTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String URL = "jdbc:sqlserver://localhost;Database=student";
Connection con = null;
ResultSet rs = null;
Statement st = null;
String sql = "select * from student";
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
con = DriverManager.getConnection(URL, "sa", "dlut_200892015");
st = con.createStatement();
rs = st.executeQuery(sql);
if(rs!=null)
{
ResultSetMetaData rsmd = rs.getMetaData();
int countCols = rsmd.getColumnCount();
for(int i=1;i<=countCols;i++)
{
if(i>1)System.out.print("; ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while(rs.next())
{
System.out.print(rs.getString("cno")+" ");
System.out.print(rs.getString("cname")+" ");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(rs!=null)rs.close();
if(st!=null)st.close();
if(con!=null)con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Windows身份验证模式连接SQL Server2008数据库代码:
import java.sql.*;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
public class sqlTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String URL = "jdbc:sqlserver://localhost;integratedSecurity=true;Database=student";
Connection con = null;
ResultSet rs = null;
Statement st = null;
String sql = "select * from student";
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
con = DriverManager.getConnection(URL);
st = con.createStatement();
rs = st.executeQuery(sql);
if(rs!=null)
{
ResultSetMetaData rsmd = rs.getMetaData();
int countCols = rsmd.getColumnCount();
for(int i=1;i<=countCols;i++)
{
if(i>1)System.out.print("; ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while(rs.next())
{
System.out.print(rs.getString("cno")+" ");
System.out.print(rs.getString("cname")+" ");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(rs!=null)rs.close();
if(st!=null)st.close();
if(con!=null)con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}