在搭建Java EE环境中如何连接SQl server 2005数据库 这一学期刚学习Java EE,在搭建其环境时,不知该如何连接数据库,另外就是环境变量不该如何配置,请各位高手指导指导。 我用的开发环境是jdk1.6+tomcat 6.0+myeclipse+SQl server 2005. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在项目的包中导入相应的JDBC驱动包,然后把下面的代码放在一个放在class里面,一个放在jdbc.properties里面,注意jdbc.properties放在src目录下,然后直接调用getConnection()获取连接,另外请修改jdbc.properties中的设置。package ibatis.test.model;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import java.util.Properties;/** * @author king * jdbc连接工厂 * */public class JdbcFactory { private Connection conn = null; private static final String FILEPATH="src/jdbc.properties"; private Connection connectionFactory(String filePath) throws Exception{ Map<String, Object> params=new HashMap<String, Object>(); try { params = readProperties(filePath); } catch (Exception e) { throw new Exception("文件不存在,请检查文件目录"); } int type = checkProperties(filePath); switch (type) { case 1:return getJdbcConnection(params.get("driver_class"),params.get("url"),params.get("username"),params.get("password")); case 2:return getDatasourceConnection(params.get("datasource")); case 3:return null; default: throw new Exception("错误的连接方式"); } } public static Connection getConnection()throws Exception{ return new JdbcFactory().connectionFactory(FILEPATH); } /** * 以Datasource方式获取连接 * @param datasource * @return */ private Connection getDatasourceConnection(Object datasource) { //TODO Datasource方式连接 return null; } /** * 以JDBC方式获取连接 * @param driver_class * @param url * @param username * @param password * @return * @throws Exception */ private Connection getJdbcConnection(Object driver_class, Object url,Object username, Object password)throws Exception { String driver = (String) driver_class; String jdbc_url = (String) url; String jdbc_user = (String) username; String jdbc_pass = (String) password; try { Class.forName(driver); conn = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_pass); return conn; } catch (ClassNotFoundException e) { throw e; } catch (SQLException e) { throw e; } } /** * 验证配置文件设置 * dbms;DBMS类型 * connecttype连接方式 可指定为:jdbc或者datasource以及provider方式 * 当指定了参数connecttype为jdbc时,以下参数必需提供 * driver_class; * url; * username; * password; * 当指定了参数connecttype为datasource时,以下参数必需提供 * datasource; * @return * 1 jdbc * 2 datasource * 3 provider * @throws Exception */ private int checkProperties(String filePath)throws Exception{ Map<String, Object> params=new HashMap<String, Object>(); int type=0; try { try { params = readProperties(filePath); } catch (Exception e) { throw new Exception("文件不存在,请检查文件目录"); } if(params!=null){ if(params.containsKey("dbms")){ if(params.get("dbms").equals("")){ throw new Exception("DBMS类型:dbms不能为空"); } } if(params.containsKey("connecttype")){ if(params.get("connecttype").equals("")){ throw new Exception("连接方式:connecttype不能为空"); }else{ if(params.get("connecttype").equals("jdbc")){ type = 1; if(params.containsKey("driver_class")){ if(params.get("driver_class").equals("")){ throw new Exception(params.get("connecttype")+"连接方式下driver_class不能为空"); } } if(params.containsKey("url")){ if(params.get("url").equals("")){ throw new Exception(params.get("connecttype")+"连接方式下url不能为空"); } } if(params.containsKey("username")){ if(params.get("username").equals("")){ throw new Exception(params.get("connecttype")+"连接方式下username不能为空"); } } if(params.containsKey("password")){ if(params.get("password").equals("")){ throw new Exception(params.get("connecttype")+"连接方式下password不能为空"); } } }else if(params.get("connecttype").equals("datasource")){ type = 2; if(params.containsKey("datasource")){ if(params.get("datasource").equals("")){ throw new Exception(params.get("connecttype")+"连接方式下datasource不能为空"); } } }else if(params.get("connecttype").equals("provider")){ type = 3; throw new Exception("暂时未添加此连接方式"); }else{ throw new Exception("连接方式:connecttype可指定为:jdbc或者datasource以及provider方式"); } } } } return type; } catch (Exception e) { throw e; } } /** * 根据文件路径和key读取对应的value * @param key * @param filePath 文件路径 * @return 可对应value * @throws Exception 文件不存在或者错误的key */ @SuppressWarnings("unused") private String readValue(String key,String filePath ) throws Exception{ Properties props = new Properties(); String value=""; try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); value = props.getProperty(key); return value; } catch (Exception e) { throw new Exception("文件不存在或错误的key,请检查文件路径和key是否正确"); } } /** * 读取properties的全部信息 * @param filePath */ @SuppressWarnings("unchecked") private Map<String, Object> readProperties(String filePath)throws Exception { Map<String, Object> params=new HashMap<String, Object>(); Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = props.getProperty(key)==null?"":props.getProperty(key); params.put(key, Property); } return params; } catch (Exception e) { throw new Exception("文件不存在,请检查文件路径"); } }}#DBMS类型:sqlserver,mysql,oracledbms=sqlserver#connecttype 连接方式,可指定为:jdbc或者datasource以及provider方式connecttype=jdbc#当指定了参数connecttype为jdbc时,以下参数必需提供driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver://localhost:1039;DatabaseName=kingtbls;SelectMethod=cursorusername=kingpassword=king#当指定了参数connecttype为datasource时,以下参数必需提供datasource= 环境变量的设置:java_home D:\DevelopEdition\lib\Jar\jdk1.6.0_18;path %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;classpath .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar分别在cmd窗口用 java -version;java;javac;来验证是否成功,java_home的配置自己修改成你的jdk路径,其他可照抄,注意classpath前的 .;不要省略,如果这些项已经有系统环境变量就把他们放在最前面 tomcat 6 不是 J2EE 环境呀 如何通用解析Hibernate返回结果集List 问一个线程问题 使用OpenJPA,可以向数据库中插入数据,但一读数据就出错! JSP如何获得服务名 救命~~!图书管理系统问题 No action instance for path /InsertNewNotice could be created 从一个人的艰辛历程中寻找学习J2EE的捷径 在servlet中怎么得到web.xml的参数 structs中上传图片? 请教JBoss_Tomcat如何作为插件加到eclipse中,用文档资料吗? hibernate分页时一个奇怪的问题!不知道大家遇见过没有 这些内容在哪可以查到
,另外请修改jdbc.properties中的设置。
package ibatis.test.model;import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;/**
* @author king
* jdbc连接工厂
*
*/
public class JdbcFactory { private Connection conn = null;
private static final String FILEPATH="src/jdbc.properties"; private Connection connectionFactory(String filePath) throws Exception{
Map<String, Object> params=new HashMap<String, Object>();
try {
params = readProperties(filePath);
} catch (Exception e) {
throw new Exception("文件不存在,请检查文件目录");
}
int type = checkProperties(filePath);
switch (type) {
case 1:return getJdbcConnection(params.get("driver_class"),params.get("url"),params.get("username"),params.get("password"));
case 2:return getDatasourceConnection(params.get("datasource"));
case 3:return null;
default: throw new Exception("错误的连接方式");
} } public static Connection getConnection()throws Exception{
return new JdbcFactory().connectionFactory(FILEPATH);
} /**
* 以Datasource方式获取连接
* @param datasource
* @return
*/
private Connection getDatasourceConnection(Object datasource) {
//TODO Datasource方式连接
return null;
} /**
* 以JDBC方式获取连接
* @param driver_class
* @param url
* @param username
* @param password
* @return
* @throws Exception
*/
private Connection getJdbcConnection(Object driver_class, Object url,Object username, Object password)throws Exception {
String driver = (String) driver_class;
String jdbc_url = (String) url;
String jdbc_user = (String) username;
String jdbc_pass = (String) password;
try {
Class.forName(driver);
conn = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_pass);
return conn;
} catch (ClassNotFoundException e) {
throw e;
} catch (SQLException e) {
throw e;
}
} /**
* 验证配置文件设置
* dbms;DBMS类型
* connecttype连接方式 可指定为:jdbc或者datasource以及provider方式
* 当指定了参数connecttype为jdbc时,以下参数必需提供
* driver_class;
* url;
* username;
* password;
* 当指定了参数connecttype为datasource时,以下参数必需提供
* datasource;
* @return
* 1 jdbc
* 2 datasource
* 3 provider
* @throws Exception
*/
private int checkProperties(String filePath)throws Exception{
Map<String, Object> params=new HashMap<String, Object>();
int type=0;
try {
try {
params = readProperties(filePath);
} catch (Exception e) {
throw new Exception("文件不存在,请检查文件目录");
}
if(params!=null){
if(params.containsKey("dbms")){
if(params.get("dbms").equals("")){
throw new Exception("DBMS类型:dbms不能为空");
}
}
if(params.containsKey("connecttype")){
if(params.get("connecttype").equals("")){
throw new Exception("连接方式:connecttype不能为空");
}else{
if(params.get("connecttype").equals("jdbc")){
type = 1;
if(params.containsKey("driver_class")){
if(params.get("driver_class").equals("")){
throw new Exception(params.get("connecttype")+"连接方式下driver_class不能为空");
}
}
if(params.containsKey("url")){
if(params.get("url").equals("")){
throw new Exception(params.get("connecttype")+"连接方式下url不能为空");
}
}
if(params.containsKey("username")){
if(params.get("username").equals("")){
throw new Exception(params.get("connecttype")+"连接方式下username不能为空");
}
}
if(params.containsKey("password")){
if(params.get("password").equals("")){
throw new Exception(params.get("connecttype")+"连接方式下password不能为空");
}
}
}else if(params.get("connecttype").equals("datasource")){
type = 2;
if(params.containsKey("datasource")){
if(params.get("datasource").equals("")){
throw new Exception(params.get("connecttype")+"连接方式下datasource不能为空");
}
}
}else if(params.get("connecttype").equals("provider")){
type = 3;
throw new Exception("暂时未添加此连接方式"); }else{
throw new Exception("连接方式:connecttype可指定为:jdbc或者datasource以及provider方式");
}
}
}
}
return type;
} catch (Exception e) {
throw e;
}
} /**
* 根据文件路径和key读取对应的value
* @param key
* @param filePath 文件路径
* @return 可对应value
* @throws Exception 文件不存在或者错误的key
*/
@SuppressWarnings("unused")
private String readValue(String key,String filePath ) throws Exception{
Properties props = new Properties();
String value="";
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
value = props.getProperty(key);
return value;
} catch (Exception e) {
throw new Exception("文件不存在或错误的key,请检查文件路径和key是否正确");
} } /**
* 读取properties的全部信息
* @param filePath
*/
@SuppressWarnings("unchecked")
private Map<String, Object> readProperties(String filePath)throws Exception {
Map<String, Object> params=new HashMap<String, Object>();
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key)==null?"":props.getProperty(key);
params.put(key, Property);
}
return params;
} catch (Exception e) {
throw new Exception("文件不存在,请检查文件路径");
}
}}
#DBMS类型:sqlserver,mysql,oracle
dbms=sqlserver#connecttype 连接方式,可指定为:jdbc或者datasource以及provider方式
connecttype=jdbc#当指定了参数connecttype为jdbc时,以下参数必需提供
driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1039;DatabaseName=kingtbls;SelectMethod=cursor
username=king
password=king#当指定了参数connecttype为datasource时,以下参数必需提供
datasource=
java_home D:\DevelopEdition\lib\Jar\jdk1.6.0_18;
path %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
classpath .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar分别在cmd窗口用 java -version;java;javac;来验证是否成功,java_home的配置自己修改成你的jdk路径,其他可照抄,注意classpath前的 .;不要省略,如果这些项已经有系统环境变量就把他们放在最前面