连接sql2000小例子

1、加入spring的常用jar和sql2000直连驱动
2、配置文件,使用c3p0-0.9.1.2.jar
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--使用c3p0配置数据源-->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- mysql的配置      
			driverClass:com.mysql.jdbc.Driver      
			jdbcUrl:jdbc:mysql://localhost/springdemo      
		-->
		<!-- oracle的配置      
			driverClass:oracle.jdbc.driver.OracleDriver   
			jdbcUrl:jdbc:oracle:thin:@localhost:1521:springdemo      
		-->
		<property name="driverClass"
			value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
		<property name="jdbcUrl"
			value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=springdemo" />
		<property name="user" value="sa" />
		<property name="password" value="" />
		<property name="maxPoolSize" value="40" />
		<property name="minPoolSize" value="1" />
		<property name="initialPoolSize" value="1" />
		<property name="maxIdleTime" value="20" />
	</bean>
</beans>


也可以使用apache的dbcp配置数据源,则加入commons-dbcp.jar和commons-pool.jar,相应的代码改为
<bean id="jdbcDataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
		<property name="url"
			value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=springdemo" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>


说明:数据库为springdemo
3、测试文件
package com.modellite.spring;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class NationalApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		DataSource ds=(DataSource)ctx.getBean("dataSource");
		Connection conn=ds.getConnection();
		Statement stmt=conn.createStatement();
		stmt.execute("insert into tb_users values('lqlq','123456789')");
		if(stmt!=null){
			stmt.close();
		}
		if(conn!=null){
			conn.close();
		}
		
	}

}


说明:tb_users为用户表,结构为uid,uname,upassword,uid为自动增长列。

你可能感兴趣的:(spring,oracle,sql,mysql,jdbc)