package cn.com.zzb.eshopping.db;
import java.sql.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBConnect {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rst;
private String str;
/**
* 获得数据库的一个Connection连接
*/
private void init()
{
try
{
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//String url = "jdbc:mysql://localhost/mydb1";
//conn = DriverManager.getConnection(url,"root","12345");
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//*******************************************
/**
* 不带参数的构造函数
*/
public DBConnect()
{
try
{
init(); //获得一个数据库连接
stmt = conn.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 带s参数的构造函数
*/
public DBConnect(String s)
{
try
{
init();
pstmt = conn.prepareStatement(s);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 批处理sql语句
*/
public DBConnect(StringBuffer sb)
{
try
{
init();
stmt = conn.createStatement();
stmt.addBatch(sb.toString());
}
catch(Exception e)
{
e.printStackTrace();
}
}
// *******************************************
/**
* 执行数据库查询语句,s为sql语句,把查询结果赋给ResultSet
*/
public void excuteQuery(String s)
{
try
{
if(stmt != null)
{
rst = stmt.executeQuery(s);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void excuteQuery()
{
try
{
if(pstmt != null)
{
rst = pstmt.executeQuery();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 对数据库进行update操作
*/
public int excuteUpdate(String s)
{
int status = 0;
try
{
if(stmt != null)
status = stmt.executeUpdate(s);
}
catch(Exception e)
{
e.printStackTrace();
}
return status;
}
public int excuteUpdate()
{
int status = 0;
try
{
if(pstmt != null)
{
status = pstmt.executeUpdate();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return status;
}
public int[] excuteBatch()
{
int[] status = null;
try
{
if(stmt != null)
{
status = stmt.executeBatch();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return status;
}
//*************************************************
/**
*
*/
public void setString(int i,String s)
{
try
{
pstmt.setString(i,s);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setBoolean(int i, boolean flag)
{
try{ pstmt.setBoolean(i, flag);}
catch(Exception e) { e.printStackTrace();
}
}
public void setByte(int i, byte byte0)
{
try{pstmt.setByte(i, byte0);}
catch(Exception e){
e.printStackTrace();
}
}
public void setBytes(int i, byte abyte0[])
{
try{pstmt.setBytes(i, abyte0);}
catch(Exception e){ e.printStackTrace();
}
}
public void setDate(int i, Date date)
{
try{pstmt.setDate(i, date);}
catch(Exception e) { e.printStackTrace();
}
}
public void setTime(int i, Time time)
{
try{pstmt.setTime(i,time);}
catch(Exception e) { e.printStackTrace();
}
}
public void setShort(int i, short word0)
{
try{pstmt.setShort(i,word0);}
catch(Exception e) {e.printStackTrace();
}
}
public void setInt(int i, int j)
{
try{pstmt.setInt(i,j);}
catch(Exception e) { e.printStackTrace();
}
}
public void setLong(int i, long l)
{
try{pstmt.setLong(i, l);}
catch(Exception e) { e.printStackTrace();
}
}
public void setFloat(int i, float f)
{
try{pstmt.setFloat(i, f);}
catch(Exception e) { e.printStackTrace();
}
}
public void setDouble(int i, double d)
{
try{pstmt.setDouble(i, d);}
catch(Exception e) {e.printStackTrace();}
}
//******************************************
/**
*
*/
public boolean getBoolean(int i)
throws Exception
{
return rst.getBoolean(i);
}
public boolean getBoolean(String s)
throws Exception
{
return rst.getBoolean(s);
}
public byte getByte(int i)
throws Exception
{
return rst.getByte(i);
}
public byte getByte(String s)
throws Exception
{
return rst.getByte(s);
}
public byte[] getBytes(int i)
throws Exception
{
return rst.getBytes(i);
}
public byte[] getBytes(String s)
throws Exception
{
return rst.getBytes(s);
}
public Date getDate(int i)
throws Exception
{
return rst.getDate(i);
}
public Date getDate(String s)
throws Exception
{
return rst.getDate(s);
}
public Time getTime(int i)
throws Exception
{
return rst.getTime(i);
}
public Time getTime(String s)
throws Exception
{
return rst.getTime(s);
}
public double getDouble(int i)
throws Exception
{
return rst.getDouble(i);
}
public double getDouble(String s)
throws Exception
{
return rst.getDouble(s);
}
public float getFloat(int i)
throws Exception
{
return rst.getFloat(i);
}
public float getFloat(String s)
throws Exception
{
return rst.getFloat(s);
}
public int getInt(int i)
throws Exception
{
return rst.getInt(i);
}
public int getInt(String s)
throws Exception
{
return rst.getInt(s);
}
public long getLong(int i)
throws Exception
{
return rst.getLong(i);
}
public long getLong(String s)
throws Exception
{
return rst.getLong(s);
}
public short getShort(int i)
throws Exception
{
return rst.getShort(i);
}
public short getShort(String s)
throws Exception
{
return rst.getShort(s);
}
public String getString(int i)
throws Exception
{
return rst.getString(i);
}
public String getString(String s)
throws Exception
{
return rst.getString(s);
}
//***************************************
/**
*
*/
public boolean next()
{
try
{
return rst.next();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public void close()
{
try
{
if(conn !=null)
conn.close();
if(pstmt !=null)
pstmt.close();
if(stmt !=null)
stmt.close();
if(rst != null)
rst.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JSP经典代码
共50个文件
gif:12个
class:9个
java:9个
需积分: 0 161 下载量 158 浏览量
2007-12-02
15:19:09
上传
评论
收藏 44KB RAR 举报
温馨提示
JSP经典代码 推荐
资源推荐
资源详情
资源评论
收起资源包目录
JSP.rar (50个子文件)
JSP
.project 385B
bin
eshopping
WEB-INF
weblogic.xml 771B
undeploy.xml 243B
web.xml 2KB
deploy.xml 239B
targets.xml 71B
resin-web.xml 292B
lib
classes
cn
com
zzb
eshopping
db
DBConnect.class 7KB
testdb.class 890B
model
User.class 425B
sql
SqlUser.class 2KB
SqlUserFactory.class 941B
SqlFactory.class 838B
UserFactory.class 2KB
Factory.class 1008B
util
GetRandomCode.class 707B
build.xml 3KB
build.properties 412B
web.xml.bak 2KB
index.jsp 1KB
META-INF
MANIFEST.MF 168B
logon
logon.jsp 3KB
logout.jsp 334B
chklogon.jsp 1KB
include
CSS.CSS 617B
image.jsp 2KB
error.jsp 347B
images
username.gif 157B
bg.gif 309B
username2.gif 157B
userlogin2.gif 448B
reg_button.gif 136B
liuchen.gif 3KB
log_button.gif 146B
userps2.gif 144B
Thumbs.db 24KB
userps.gif 144B
userlogin.gif 865B
userym.gif 291B
userym2.gif 331B
j2src
cn
com
zzb
eshopping
db
DBConnect.java 7KB
model
Factory.java 538B
User.java 573B
sql
SqlUser.java 978B
SqlUserFactory.java 473B
SqlFactory.java 430B
UserFactory.java 2KB
testdb.java 560B
util
GetRandomCode.java 473B
.classpath 2KB
共 50 条
- 1
资源评论
prettywave
- 粉丝: 5
- 资源: 73
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功