寫java web應用時需要連接sqlserver數據庫的步驟如下:
- 安裝tomcat。
- 打開myeclipse,需要把自己安裝的jdk與tomcat都引入,不要用自帶的。(這一步很重要)
- jdk配置:
- window --> preferences
- Java --> Installed JREs
- 先不管當前的配置,選擇ADD
- ADD之後選擇standard VM,然後點擊next
- 在新彈出框中,這裏就要用到Java home和jre name,這是配置Java環境是設置好了的
- window --> preferences
- tomcat配置
- window --> preferences
- Servers->Tomcat,選擇對應版本,選擇本地路徑即可。
- 下載sqljdbc4.jar。只需要這一個jar包就夠了。
- 把sqljdbc4.jar放在對應的4個目錄:1)/java/jre/lib/ext 2)/java/jdk/jre/lib/ext 3)/tomcat/lib 4)對應java web項目中的lib
- 在 mybatis.xml的配置文件中編寫配置。其中,紅色字體部分就是tomcat對應的Resource name。
- 在mybatis配置中,dao包中的接口類與配置文件名字一定要相同。接口類需要加上@Repository
- 在mybatis配置中,domain包中的對象類一定要進行序列化,即實現Serializable接口,並重寫toString()函數。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:/comp/env/sqlserver" /> </bean> <!-- 註解方式實現事務 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- define the SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.invoice.api.domain"/> </bean> <!-- scan for mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="basePackage" value="com.invoice.service.dao" /> </bean> </beans>
- 修改tomcat中的配置
- 在/tomcat/conf/context.xml文件中,在標籤<Context>下新增子標籤<ResourceLink>
<ResourceLink name="sqlserver" global="sqlserver" type="javax.sql.DataSource" />
- 在/tomcat/conf/server.xml文件中,在標籤<GlobalNamingResources>下新增子標籤<Resource>
以上兩步就足夠tomcat的配置。<Resource name="sqlserver" auth="Container" type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://127.0.0.1:1433;databaseName=test" username="test" password="000000" maxActive="30" maxIdle="5" maxWait="10000" />
遇到的問題
- HTTP 405 (這是因爲HTTP請求中的GET/POST方法弄混了,405的意思是指找到了服務器,也找到了對應的方法入口,但是調用的方法沒有匹配成功)
- 把項目搬過來的時候,報錯【Name jdbc is not bound in this Context或者是Cannot create JDBC driver of class '' for connect URL 'null'】。(這就是上下文沒有匹配到JDBC.在myeclipse中不要用自帶的tomcat,把自己新建的TOMCAT配進去就行)
- 在mybatis裏整型變量比較需要加.toString(),如<if test ="num == '0'.toString()">
- 還是不明白java:/comp/env/這個變量到底在哪裏聲明的?
- sqlserver在tomcat中連接url的配置格式是:Oracle、Mysql和SQL Server數據庫連接的URL寫法
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:sqlserver://localhost:1433;databaseName=mydb";
//mydb爲數據庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);