1. JDBC连接数据库
2. C3P0连接数据库
3. DBCP连接数据库
4. Tomcat数据源
5. JBoss数据源
6. WebLogic数据源
7. Connection操作数据库
8. JDBC事务处理
9. Tomcat JTA
10. JBoss JTA
11. WebLogic JTA
1. JDBC连接数据库
直接使用JDBC API获得Connection连接。
public class ConnectionUtil {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/txazo";
private static final String USER = "root";
private static final String PASSWORD = "root";
static {
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
2. C3P0连接数据库
C3P0连接池。
public class ConnectionUtil {
private static ComboPooledDataSource dataSource = null;
static {
try {
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1/txazo");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setMinPoolSize(2);
dataSource.setMaxPoolSize(20);
dataSource.setInitialPoolSize(4);
} catch (Exception e) {
e.printStackTrace();
}
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
3. DBCP连接数据库
DBCP连接池。
public class ConnectionUtil {
private static BasicDataSource dataSource = null;
static {
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1/txazo");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setMaxActive(20);
dataSource.setInitialSize(4);
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
4. Tomcat数据源
1)TOMCAT_HOME/conf/context.xml的<Context>节点下添加如下内容:
<Resource name="jdbc/txazo" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/txazo"/>
2)WEB-INF/web.xml中添加如下内容:
<resource-ref>
<res-ref-name>jdbc/txazo</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3)Connection连接工具类
public class ConnectionUtil {
private static DataSource dataSource = null;
static {
Context context = null;
try {
context = new InitialContext();
dataSource = (DataSource) context
.lookup("java:comp/env/jdbc/txazo");
} catch (Exception e) {
e.printStackTrace();
}
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
5. JBoss数据源
1)配置数据源
JBOSS_HOME/docs/examples/jca/mysql-ds.xml复制到JBOSS_HOME/server/default/deploy下,重命名为txazo-ds.xml,并修改内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>txazo</jndi-name>
<connection-url>jdbc:mysql://127.0.0.1:3306/txazo</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
2)Connection连接工具类
public class ConnectionUtil {
private static DataSource dataSource = null;
static {
Context context = null;
try {
context = new InitialContext();
dataSource = (DataSource) context.lookup("java:/txazo");
} catch (Exception e) {
e.printStackTrace();
}
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
6. WebLogic数据源
1)配置数据源
使用WebLogic控制台配置数据源txazo。
2)Connection连接工具类
public class ConnectionUtil {
private static DataSource dataSource = null;
static {
Context context = null;
try {
context = new InitialContext();
dataSource = (DataSource) context.lookup("txazo");
} catch (Exception e) {
e.printStackTrace();
}
}
private ConnectionUtil() {
}
public static synchronized Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
7. Connection操作数据库
1)增删改操作
Connection connection = null;
PreparedStatement statement = null;
try {
connection = ConnectionUtil.getConnection();
statement = connection.prepareStatement("delete from user");
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2)查询操作
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = ConnectionUtil.getConnection();
statement = connection.prepareStatement("select * from user");
resultSet = statement.executeQuery();
while (resultSet.next()) {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
8. JDBC事务处理
Connection connection = null;
PreparedStatement statement = null;
try {
connection = ConnectionUtil.getConnection();
connection.setAutoCommit(false);
statement = connection.prepareStatement("delete from user");
statement.executeUpdate();
connection.commit();
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (Exception exception) {
exception.printStackTrace();
}
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
9. Tomcat JTA
1)下载JTOM,添加相应Jar包到TOMCAT_HOME/lib下。
2)TOMCAT_HOME/lib/carol.properties:
carol.protocols=jrmp
carol.jvm.rmi.local.call=true
carol.start.jndi=false
carol.start.ns=false
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
3)TOMCAT_HOME/conf/context.xml的<Context>节点下添加如下内容:
<Resource name="jdbc/txazo" auth="Container" type="javax.sql.DataSource"
factory="org.objectweb.jotm.datasource.DataSourceFactory"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/txazo"/>
<Resource name="UserTransaction"
auth="Container"
type="javax.transaction.UserTransaction" />
<Transaction factory="org.objectweb.jotm.UserTransactionFactory"
jotm.timeout="60" />
4)JTA编程事务
UserTransaction transaction = null;
Connection connection = null;
PreparedStatement statement = null;
try {
Context context = new InitialContext();
transaction = (UserTransaction) context.lookup("java:comp/UserTransaction");
transaction.begin();
connection = ConnectionUtil.getConnection();
connection.setAutoCommit(false);
statement = connection.prepareStatement("delete from user");
statement.executeUpdate();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
try {
transaction.rollback();
} catch (Exception exception) {
exception.printStackTrace();
}
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
10. JBoss JTA
JBoss中JTA事务管理器的JNDI名为UserTransaction。
UserTransaction transaction = null;
Connection connection = null;
PreparedStatement statement = null;
try {
Context context = new InitialContext();
transaction = (UserTransaction) context.lookup("UserTransaction");
transaction.begin();
connection = ConnectionUtil.getConnection();
connection.setAutoCommit(false);
statement = connection.prepareStatement("delete from user");
statement.executeUpdate();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
try {
transaction.rollback();
} catch (Exception exception) {
exception.printStackTrace();
}
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
11. WebLogic JTA
WebLogic中JTA事务管理器的JNDI名为javax.transaction.UserTransaction。
UserTransaction transaction = null;
Connection connection = null;
PreparedStatement statement = null;
try {
Context context = new InitialContext();
transaction = (UserTransaction) context
.lookup("javax.transaction.UserTransaction");
transaction.begin();
connection = ConnectionUtil.getConnection();
connection.setAutoCommit(false);
statement = connection.prepareStatement("delete from user");
statement.executeUpdate();
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
try {
transaction.rollback();
} catch (Exception exception) {
exception.printStackTrace();
}
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
分享到:
相关推荐
JSP+Servlet+JavaBean+JDBC示例程序
JSP+Servlet+Javabean+Mysql 网上书店管理系统.zip
5.Servlet + JSP +JavaBean + JDBC(DBUtils)+ mysql 6.数据库 create database day19; use day19; create table t_customer( id varchar(40) primary key, username varchar(20), gender varchar(10), ...
采用jsp+servlet+javabean,jsp负责少量的页面处理,以servlet为主。 结构清晰明了,未对页面做美工处理,因此html和javascript代码很少, 详细介绍了jdbc的常用api以及数据库中的表的级联。 开发调试平台...
实验一 JSP+Servlet+Javabean+jdbc-基于MVC模式的数据库的应用 实验二 JSF+JPA实现用户注册 实验三 struts+spring+hibernate框架访问数据库的应用 实验四 使用WebService和EJBean访问数据库的应用
网上商城所有基本功能实现、 包含所有图片等资源 包含数据库创建脚步 开发环境 jdk1.7 myeclipse10 tomcat6.0 mysql 5
适合首次接触jdbc数据库连接的初学者 完全代码 可运行
其中Servlet担当主要逻辑控制,通过接受Jsp传来的用户请求,调用以及初始化JavaBean,在通过Jsp传到客户端,本系统中SqlBean担当主要的与数据库的连接与通信,JavaBean在本系统中主要担当配合Jsp以及Servlet来完成...
jsp+servlet+javabean案例,包括JDBC数据库连接,javabean数据封装,使用了简单的MVC框架
简单的 Servlet+javabean+Oracle用户登录系统,使用了dao连接数据库,方便大家借鉴和学习
前台界面用jsp+servlet+javabean(模式二)的模式开发,后台用jsp+javabean(模式一)模式开,后台还用了一个smartupload上传组件,JDBC桥连接数据库(sql2000).
用jsp+servlet+javaBean连接数据库的登录
jsp+servlet+javabean实现的学校教务管理系统 程序说明: (1)系统测试环境: Windows xp sp2 + myEclipse5.5 + Tomcat6.0 + SQLServer2000 + JDK1.5 (2)数据库文件:数据库备份目录下的文件,自己在sqlserver2000...
其中Servlet担当主要逻辑控制,通过接受Jsp传来的用户请求,调用以及初始化JavaBean,在通过Jsp传到客户端,本系统中SqlBean担当主要的与数据库的连接与通信,JavaBean在本系统中主要担当配合Jsp以及Servlet来完成...
统名称:jsp+servlet+javabean新闻发布系统 大家好,这是我搜集整理好并且测试通过的的一个新闻发布系统,有程序,设计详细文档和ppt演示,我现在把程序共享给大家,有需要完整文档的朋友请再联系我!! 系统角色:...
其中Servlet担当主要逻辑控制,通过接受Jsp传来的用户请求,调用以及初始化JavaBean,在通过Jsp传到客户端,本系统中SqlBean担当主要的与数据库的连接与通信,JavaBean在本系统中主要担当配合Jsp以及Servlet来完成...
sp+servlet+javabean实现的学校教务管理系统 程序说明: (1)系统测试环境: Windows xp sp2 + myEclipse5.5 + Tomcat6.0 + SQLServer2000 + JDK1.5 (2)数据库文件:数据库备份目录下的文件,自己在sqlserver2000...
这是一个以JSP、servlet、JavaBean实现MVC三层架构的简单例子,使用XML作为数据库连接的配置文件。 使用环境:eclipse_3.2、myeclipse_5.1、jdk_6.0、tomcat_5.5、sql server 2005 <br>----------------------...
用JSP+JavaBean+Servlet开发的数据库应用实例——CD管理系统,有完整的工程源码,并且利用它演示在Servlet中连接和使用MySQL数据库。
其中Servlet担当主要逻辑控制,通过接受Jsp传来的用户请求,调用以及初始化JavaBean,在通过Jsp传到客户端,本系统中SqlBean担当主要的与数据库的连接与通信,JavaBean在本系统中主要担当配合Jsp以及Servlet来完成...