DB处理
来源:互联网 发布:网络资源库尔雅 编辑:程序博客网 时间:2024/06/11 17:00
DB处理
1. 获得连接:
public static Connection getConn() throws NamingException, SQLException {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/fee_db");
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
return conn;
}
通过Jndi访问数据库的
2. 关闭连接:
public static void closeDB(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
保存在DBUtil类中。
3. 执行语句:
3.1 PreparedStatement
public boolean checkLogin(String user_name, String password, Connection conn)
throws Exception {
PreparedStatement pst = null; //a
StringBuffer sql = new StringBuffer(
"select * from t_user u where u.user_name=? and u.pwd=? and u.emp_id is not null"); //b
boolean rtn = false;
ResultSet rst = null; //c
pst = conn.prepareStatement(sql.toString()); //d
pst.setString(1, user_name);
pst.setString(2, Encrypter.encrypt(password)); //e
rst = pst.executeQuery(); //f
if (rst.next()) {
rtn = true;
} //g
DBUtil.closeDB(pst, rst);
return rtn;
}
0.打开连接
a. 声明PreparedStatement
b. 准备带?的SQL语句
c. 准备返回的结果集
d. SQL传入PreparedStatement
e. 设置传入的参数
f. 执行SQL
g. 取结果集
0. 关闭连接
3.1 更新
4. tomcat的配置
D:/Tomcat5.0/conf/Catalina/localhost/fee_pro.xml的配置:
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" workDir="work/Catalina/localhost/feepro" path="/feepro" debug="5" docBase="F:/JavaWork/feepro/web">
<Logger className="org.apache.catalina.logger.FileLogger" suffix=".txt" prefix="localhost_feepro_log." timestamp="true"/>
<Resource type="javax.sql.DataSource" auth="Container" name="jdbc/fee_db"/>
<ResourceParams name="jdbc/fee_db">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>password</name>
<value>wwmmbb</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.168.1.100:3308/fee_db</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
</ResourceParams>
</Context>
- DB处理
- Oracle DB 处理数据
- DB-IP处理脚本
- DB排他的处理方法
- DB到出Excel 的格式化处理
- Oracle DB 管理数据并发处理
- Sun如何处理PostgreSQL和Java DB?
- 如何处理HANG住的DB
- db
- DB
- db
- DB
- db
- DB
- db
- db
- db
- DB
- zz: Directshow 虚拟摄像头实现
- ORA-12154: TNS:无法解析指定的连接标识符
- 自己开发的一些JS代码(flash式滚动图片)
- 通过临时表解决--未能为视图或函数解释分析分配辅助表,超过了查询中表地最大数目(260)--的问题
- 把一个数据库中的表的内容复制到另一数据库中的表中
- DB处理
- _记录点 交易管理完成
- 连接到其他机子上的Oracle服务
- 'Ext.EventObject' 为空或不是对象的错误原因和解决方法
- Visual Studio 2008 实用快捷键整理(2)
- Oracle中查询前10条记录(转载)
- QTP中使用Execute
- Coolite Toolkit非常棒的控件
- 接口