📄 dataconnect.java
字号:
package com.logistic.data;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.sql.Date;
//import java.text.SimpleDateFormat;
/**
****************************************************
*类名称: Db<br>
*类功能: 数据库操作 <br>
*创建: ckjava<br>
****************************************************
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DataConnect {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public static int error=0;
/***************************************************
*函数名称: getCon()<br>
*函数功能: 获取数据库连接<br>
*返回值: 无<br>
*参数说明: 无<br>
*创建: ckjava
****************************************************/
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/DBPool");
if(ds==null){
System.err.println();
System.err.println("数据连接打开+"+(++error));
}
return ds.getConnection();
}catch(SQLException e){
System.out.print(e);
throw e;
}
catch(NamingException e){
System.out.print(e);
throw e;
}
}
/***************************************************
*函数名称: getStmtread()<br>
*函数功能: 获取数据库集合<br>
*返回值: Statement
* stmt:返回数据库集合只用于SELECT语句<br>
*参数说明: 无<br>
*创建: ckjava
****************************************************/
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
System.out.println("getStmtread");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*函数名称: getRowCount()<br>
*函数功能: 返回表的行数<br>
*返回值: int count<br>
* count>0返回的行数<br>
* count=-1表名不存在或没有纪录<br>
*参数说明: String sql<br>
* sql 数据库中的表名
*创建: ckjava
****************************************************/
public int getRowCount(String sql){
int count=0;;
try{
stmt=this.getStmtread();
rs=stmt.executeQuery("SELECT COUNT(*) FROM "+sql);
rs.getMetaData();
if(rs.next()){
count=rs.getInt(1);
}else{
count=-1;
}
}catch(Exception e){
System.out.println("getRowCount");
System.out.println(e.getMessage());
count=-2;
}finally{
this.close();
}
return count;
}
/***************************************************
*函数名称: getStmt()<br>
*函数功能: 获取数据库集合<br>
*返回值: Statement
* stmt:返回数据库集合不能用于SELECT语句<br>
*参数说明: 无<br>
*创建: ckjava
****************************************************/
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
}catch(Exception e){
System.out.println("getStmt");
System.out.println(e.getMessage());
}
return stmt;
}
/***************************************************
*函数名称: getPstmt()<br>
*函数功能: 获取数据库集合<br>
*返回值: PreparedStatement
* pstmt:返回数据库预处理语句<br>
*参数说明: sql 类sql语句<br>
*创建: ckjava
****************************************************/
public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
}catch(Exception e){
System.out.println("getPstmt");
System.out.println(e.getMessage());
}
return pstmt;
}
/***************************************************
*函数名称:close()<br>
*函数功能:关闭数据库连接<br>
*返回值: void<br>
*参数说明:无<br>
*最或修改:
*创建者: ckjava
****************************************************/
public void close(){
try{
if(rs!=null)rs.close();
}catch(Exception e){
}
try{
if(stmt!=null)stmt.close();
}catch(Exception e){
}
try{
if(con!=null){
con.close();
con=null;
System.err.println();
System.err.println("数据连接关闭-"+(--error));
}
}catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}
}
/***************************************************
*函数名称: inStr()
*函数功能: 进行编码转换把ISO-8859-1转换为GB2312格式<
*返回值: tempstr
*参数说明: str 要转换的字符串
*创建: ckjava
****************************************************/
public String inStr(String str){
String tempstr=null;
if(str==null){
str="";
}else{
try{
tempstr=new String(str.getBytes("ISO-8859-1"),"GB2312");
//tempstr=str.replace('\'',(char)1);
}catch(Exception e){
System.out.println("inStr");
System.out.println(e.getMessage());
}
}
return tempstr;
}
/*--------------------做编码出库转换---------------------------*/
public String outStr(String str){
if(str==null){
str="";
}else{
try{
str=str.replace((char)1,'\'');
}catch(Exception e){
System.out.println("outStr");
System.out.println(e.getMessage());
}
}
return str;
}
/***************************************************
*函数名称: selectdata()<br>
*函数功能: 获取数据库中记录条数<br>
*返回值: k 为数据库记录条数
*参数说明: sqls 类sql语句<br>
*创建: ckjava
****************************************************/
public int selectdata(String sqls){
int k=-10;
try{
k=0;
rs=this.getStmtread().executeQuery(sqls);
while(rs.next()){
k++;
}
}catch(Exception ex){
k=-1;
System.out.println("select");
System.out.println(ex.getMessage());
this.close();
}finally{
this.close();}
return k;
}
/***************************************************
*函数名称: updata()<br>
*函数功能: 进行 添加,删除,修改<br>
*返回值: k k>0执行成功,k<0执行失败
*参数说明: sqls 类sql语句<br>
*创建: ckjava
****************************************************/
public int updata(String sqls){
int k=-10;
try{
k=0;
k=this.getStmt().executeUpdate(sqls);
}catch(Exception ex){
k=-1;
System.out.println("updata");
System.out.println(ex.getMessage());
this.close();
}finally{this.close();}
return k;
}
/***************************************************
*函数名称: StrConverDate()<br>
*函数功能: 把合法的字符串转换为Date类型<br>
*返回值: converdat (Date类型)
*参数说明: strdate 合法的String类型日期<br>
*创建: ckjava
****************************************************/
public Date StrConvertDate(String strdate)
{
Date convertdate=null;
try{
convertdate= Date.valueOf(strdate);
System.out.print("打印日期");
System.out.print(convertdate.toString());
}catch(Exception ex){ex.printStackTrace();}
return convertdate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -