45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:怎么样实现DataSourse功能?

怎么样实现DataSourse功能?

2016-08-25 18:52:22 来源:www.45fan.com 【

怎么样实现DataSourse功能?

javax.sql 包下的接口Interface DataSourse,它是一个数据源,相当于一个工厂,有了DataSourse这个数据源之后,你在程序中就可以连接真正的物理数据库了,每个数据库产商也都提供有相应的DataSourse,每个框架也都提供了相应的DataSourse...

下面介绍几种产商和框架提供获取DataSourse,连接数据库的例子:
1:Spring 的DriverManagerDataSourse
2:jakarta的BsicalDataSourse ,BsicalDataSourseFactory
3:ibatis也有数据源 SimpleDataSourse
4:hibernate 的 DriverManagerConnectionProvider

示例:下面都以连接MySql数据库为例,我的数据库名字是itcast,表名是student.


1:用jakarta的 BsicalDataSourse 创建一个DataSourse连接池,连接数据库
package cn.itcast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

public class BasicDataSourseDemo {
public static void main(String[] args) {
//获取一个BasicDataSource对象,并设置连接数据库的属性
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql:///itcast");
bds.setUsername("root");
bds.setPassword("");

Connection cn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
//建立连接
cn = bds.getConnection();
// 显示表student内容
pstmt = cn.prepareStatement("select * from student");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name") + "/t"
+ rs.getString("salary"));
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;

} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (cn != null) {
try {
cn.close();
cn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
}

//运行上面程序之前你要导入相关jar包如:commons-dbcp-1.2.1.jar,commons-pool-1.3.jar,mysql-connector-java-5.0.3-bin.jar然后建立好mysql数据库,就可以运行了,其实上面那些连接数据库的信息完全可以用Spring 来完成,可以写一个配置文件,这里就不介绍了。

2:用apache的.BasicDataSourceFactory 创建一个数据源连接数据库
package cn.itcast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class BasicDataSourseFactoryDemo {
//获取数据库连接信息,返回一个Properties对象
public Properties getProperties() {
Properties properties = new Properties();
properties.put("driverClassName", "com.mysql.jdbc.Driver");
properties.put("url", "jdbc:mysql:///itcast");
properties.put("username", "root");
properties.put("password", "");
return properties;
}
//数据库操作,用BasicDataSourceFactory来建立一个dataSourse
public void dbOperater() {
DataSource dataSourse = null;
Connection cn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
dataSourse = BasicDataSourceFactory.createDataSource(getProperties());//调用getProperties()获得一个Properties对象
cn = dataSourse.getConnection();
//显示表student内容
pstmt = cn.prepareStatement("select * from student");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name") + "/t"
+ rs.getString("salary"));
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;

} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (cn != null) {
try {
cn.close();
cn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//
public static void main(String[] args) {
BasicDataSourseFactoryDemo basicDataSourseFactoryDemo = new BasicDataSourseFactoryDemo();
basicDataSourseFactoryDemo.dbOperater();//调用dbOperater()方法,对数据库操作
}
}

3:ibatis也有数据源 SimpleDataSourse
运行下面这个程序之前先加入相应的jar包mysql-connector-java-5.0.3-bin.jar,ibatis-common-2.jar
commons-dbcp-1.2.1.jar,commons-pool-1.3.jar
package cn.itcast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.ibatis.common.jdbc.SimpleDataSource;

public class SimpleDataSourceDemo {

public static void main(String[] args) {
Properties props = new Properties();
props.put("JDBC.Driver", "com.mysql.jdbc.Driver");
props.put("JDBC.ConnectionURL", "jdbc:mysql://localhost/itcast");
props.put("JDBC.Username", "root");
props.put("JDBC.Password", "");

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
SimpleDataSource simpleDS = null;

try {
// SimpleDataSource创建一个数据源
simpleDS = new SimpleDataSource(props);
conn = simpleDS.getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement("select * from student");

rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name") + "/t"
+ rs.getString("salary"));
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (rs != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

}


//同样运行上面程序之前你要导入相关jar包如:commons-dbcp-1.2.1.jar,commons-pool-1.3.jar,mysql-connector-java-5.0.3-bin.jar然后建立好mysql数据库,就可以运行了。

4:用hibernate 的 DriverManagerConnectionProvider
package cn.itcast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.cfg.Environment;
import org.hibernate.connection.DriverManagerConnectionProvider;

public class DriverManagerConnectionProviderDemo {

public static void main(String[] args) throws Exception {

DriverManagerConnectionProvider dmcp = new DriverManagerConnectionProvider();
Properties props = new Properties();
// 下面这两种形式都可以
props.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
props.setProperty(Environment.URL, "jdbc:mysql:///itcast");
props.setProperty("hibernate.connection.username", "root");
props.setProperty("hibernate.connection.password", "");
// 调用configure()方法获取一个Properties对象,Initialize the connection provider
// from given properties.
dmcp.configure(props);
Connection cn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 显示表student内容
cn = dmcp.getConnection();
pstmt = cn.prepareStatement("select * from student");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name") + "/t"
+ rs.getString("salary"));
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;

} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if (cn != null) {
try {
cn.close();
cn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

}

以上例子都可以顺利跑通,读者在实验时如果遇到问题,可以给我留言...

 


本文地址:http://www.45fan.com/a/question/67556.html
Tags: 实现 DataSourse javax.sql
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部