package data;
import java.sql.*;
public class data {
public Connection con = null;
private String DatabaseDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String CnnStr = "jdbc:odbc:timer";
private String FirstValue="";
public data() {/////构造函数
con=Connect(con);
}
public Connection Connect(Connection con)
{
try {
if (con==null)
{
Class.forName(DatabaseDriver);
con = DriverManager.getConnection(CnnStr, "", "");
}else
{
if (con.isClosed())
{
Class.forName(DatabaseDriver);
con = DriverManager.getConnection(CnnStr, "", "");
}
}
} catch (Exception e) {}
return con;
}
//获取查询记录总数
public int executeCount(String sql) {
int num = -1;
try {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) num=rs.getInt(1);
} catch (SQLException ex) {}
return num;
}
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {}
return rs;
}
public int executeUpdate(String sql)
{
int Count=-1;
PreparedStatement pstmt=null;
try{
pstmt=con.prepareStatement(sql);
Count=pstmt.executeUpdate();
}catch(Exception ex){
}
return Count;
}
public int delete(String sql) {
int num = -1;
try {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
num = stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException ex) {}
return num;
}
public int Delete(String sql,String userid,String Obj_ID) {
int num = -1;
try {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
num = stmt.executeUpdate(sql);
stmt.close();
} catch (SQLException ex) {}
return num;
}
//关闭数据库连接
public void CloseDataBase() {
try {
con.close();
} catch (Exception end) {}
}
//创建已填充数据的下拉列表框
public String buildList(String Name,String SQL,String Value){
String str="<select name='"+Name+"'>";
str=str+"<option value='0'>请你选择</option>";
try{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(SQL);
while(rs.next()) {
if(rs.getString(1).equals(Value)){
str=str+" <option value='"+rs.getString(1)+"' selected >"+rs.getString(2)
+"</option>";
}else{
str=str+" <option value='"+rs.getString(1)+"'>"+rs.getString(2)+"</option>";
}
}
rs.close();
stmt.close();
}catch (Exception e) {}
str=str+"</select>";
return str;
}
//Name:控件的名称
//SQL:条件查询语句 如:Select RL_ID,RL_NAME FROM T_RL WHERE ....
//Value:已经选中的值
//First:是否选中第一项有效值, True表示第一项,False表示不是第一项
public String buildSelect(String Name,String SQL,String Value,boolean First) {
String str="<select name='"+Name+"' onchange='javascript:frm.submit()'>";
str=str+"<option value='' selected></option>";
try{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(SQL);
//byte f=0;
while(rs.next()) {
if (First) {
FirstValue=rs.getString(1);
str=str+" <option value='"+rs.getString(1)+"' selected >"+rs.getString(2)
+"</option>";
First=false;
}else{
if(rs.getString(1).equals(Value)){
str=str+" <option value='"+rs.getString(1)+"' selected >"+rs.getString(2)+"</option>";
}else{
str=str+" <option value='"+rs.getString(1)+"'>"+rs.getString(2)
+"</option>";
}
}
}
rs.close();
stmt.close();
}catch (Exception e) {}
if (Value.equals("*"))
{
str=str+"<option value='*' selected>显示全部</option>";
}else
{
str=str+"<option value='*'>显示全部</option>";
}
str=str+"</select>";
return str;
}
//取得第一条记录的值
public String getFirstValue()
{
return FirstValue;
}
public String getName(String SQL){
String str="";
try{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(SQL);
if(rs.next()) {
str=rs.getString(1);
}
rs.close();
stmt.close();
}catch (Exception e) {}
return str;
}
//补位函数
//str:需要补位的字符串,c:补位字符(串),n:补位后的字符串长度,flag:补位类型(1:前补位,2:后补位)
public String format(String str,String c,int n,int flag){
String FStr="";
int k=0,i=0;
if(!str.equals("")){
FStr=str;
if(n-str.length()>0){
k=n-str.length();
for(i=0;i<k;i++){
if(flag==1){
FStr=c+FStr;
}else{
FStr=FStr+c;
}
}
if(FStr.length()-n>0){
FStr=FStr.substring(0,n-1);
}
}
}
return FStr;
}
//从表中获取最大编号,只限于一个表中只有一个字段作为主键的编号
//table_id:需要自动编号的字段所在表名,id_field:需要自动编号的字段
public String getMaxID(String table_name,String id_field){
String SQL="select MAX("+id_field+") as n from "+table_name;
String max_id="";
try {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(SQL);
if (rs.next()){
max_id=rs.getString("n");
}else{
max_id="";
}
rs.close();
stmt.close();
} catch (SQLException ex) {}
return max_id;
}
//public static void main(String[] agrs){
// data d = new data();
// try{
// ResultSet rs=d.executeQuery("SELECT * FROM person_type");
// rs.next();
// System.out.print(rs.getString("type_id"));
// }catch(Exception e){}
//}
}
- 1
- 2
- 3
- 4
- 5
前往页