测试连接数据库 sql server 2008 R2
开发环境是 Eclipse
这个是纯java驱动连接数据库的方式,需要下载 jdbc的驱动程序:sqljdbc4.jar 下载址址:http://good.gd/1963755.htm
Eclipse中的项目引用jar方法为:选中左侧的项目(如 test1),右键--> Properties --> Java Build Path--> Libraries --> Add External Jars -->选中刚才下载的 .jar 文件--> OK即可
---------------------------------------------------------------------------------------------
test1.java
//import javax.swing.JOptionPane;
public class test11 {
public static void main(String[] args) {
DBHelper help=new DBHelper();
if(help.TestConn())
System.out.println("连接成功1");
else
System.out.println("连接失败1");
//JOptionPane.showMessageDialog(null, "hello world");
}
}
DBHelper.java
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class DBHelper {
Connection dbConn = null;
private Connection GetConn(String sUser,String sPwd) {
try {
String sDriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//String sDBUrl = "jdbc:sqlserver://192.168.0.74;databaseName=wakeup";
String sDBUrl = "jdbc:sqlserver://192.168.2.28\\JONSE;databaseName=wakeup";
Class.forName(sDriverName);
dbConn = DriverManager.getConnection(sDBUrl, sUser, sPwd);
} catch (Exception ex) {
//ex.printStackTrace();
System.out.println(ex.getMessage());
}
return dbConn;
}
public boolean TestConn()
{
if(GetConn("sa","***")==null)
return false;
dbConn=null;
return true;
}
}
执行结果为:
---------------------------------------------------------------------------------------------
j
j
怎么用纯java驱动方式连接SQL server 2008 R2 http://zhidao.baidu.com/question/199399735.html
Microsoft SQL Server JDBC Driver 3.0
JDBC连接SqlServer2008图解:直连(纯JAVA方式)
http://blog.csdn.net/zhouhuan965/article/details/6258035
sqljdbc2008.jar驱动 http://download.csdn.net/download/sksbbs/3460315
Java操作SQLite数据库 http://www.cxybl.com/html/bcyy/java/201110287250.html
使用纯Java JDBC驱动 程序实现数据库的连接出现错误 http://zhidao.baidu.com/question/356905375.html
如何用JAVA连接SQLite数据库? http://zhidao.baidu.com/question/206230567.html?an=0&si=9
MSDN: Sql server jdbc 纯java驱动: 使用 JDBC 驱动程序 http://msdn.microsoft.com/zh-cn/library/ms378526.aspx
eclipse添加classpath变量 http://blog.sina.com.cn/s/blog_3fe0f6250100amoh.html
通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connecti... http://apps.hi.baidu.com/share/detail/43639042
用于JDBC操作数据库的公共类 http://vipbooks.iteye.com/blog/607207
java-sql数据库连接公共类文件 http://www.ineeke.com/archives/58/
Eclipse如何设置字体大小 : windows -> preference -> general -> apperance -> colors and fonts -> basic -> text font 双击它
默认字体为:Courier New 10
Eclipse 自动缩进快捷键: 全选代码,然后 Ctrl + Shift + F
---------------------------------------------------------------------------------------------