zoukankan
html css js c++ java
java数据库操作共通类
import
java.sql.
*
;
import
java.text.
*
;
import
java.util.
*
;
import
com.jstrd.htgl.webservice.XmlUtility;
/** */
/**
*
*/
public
class
DBComm
{
public
String ClassString
=
null
;
public
String ConnectionString
=
null
;
public
String UserName
=
null
;
public
String PassWord
=
null
;
public
Connection Conn;
public
Statement Stmt;
public
DBComm()
{
//
From System.xml
ClassString
=
XmlUtility.getConfigValue(
"
DB_ClassString
"
);
//
"oracle.jdbc.driver.OracleDriver";
ConnectionString
=
XmlUtility.getConfigValue(
"
DB_ConnectionString
"
);
//
"jdbc:oracle:thin:@192.168.103.171:1521:jstrd";
UserName
=
XmlUtility.getConfigValue(
"
DB_UserName
"
);
//
"htgl";
PassWord
=
XmlUtility.getConfigValue(
"
DB_PassWord
"
);
//
"1";
//
For ODBC
//
ClassString="sun.jdbc.odbc.JdbcOdbcDriver";
//
ConnectionString=("jdbc:odbc:DBDemo");
//
UserName="dbdemo";
//
PassWord="dbdemo";
//
For Access Driver
//
ClassString="sun.jdbc.odbc.JdbcOdbcDriver";
//
ConnectionString=("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\dbdemo.mdb;ImplicitCommitSync=Yes;MaxBufferSize=512;MaxScanRows=128;PageTimeout=5;SafeTransactions=0;Threads=3;UserCommitSync=Yes;").replace('\\','/');
//
For SQLServer Driver
//
ClassString="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//
ConnectionString="jdbc:microsoft:sqlserver:
//
127.0.0.1:1433;DatabaseName=DBDemo;User=dbdemo;Password=dbdemo";
//
UserName="dbdemo";
//
PassWord="dbdemo";
//
For Oracle Driver
//
ClassString="oracle.jdbc.driver.OracleDriver";
//
ConnectionString="jdbc:oracle:thin:@192.168.103.171:1521:jstrd";
//
UserName="htgl";
//
PassWord="1";
//
For MySQL Driver
//
ClassString="org.gjt.mm.mysql.Driver";
//
ConnectionString="jdbc:mysql:
//
localhost/softforum?user=
&password=
&useUnicode=true&characterEncoding=8859_1";
//
ClassString="com.mysql.jdbc.Driver";
//
ConnectionString="jdbc:mysql:
//
localhost/dbdemo?user=root&password=&useUnicode=true&characterEncoding=gb2312";
//
For Sybase Driver
//
ClassString="com.sybase.jdbc.SybDriver";
//
ConnectionString="jdbc:sybase:Tds:localhost:5007/tsdata";
//
tsdata为你的数据库名
//
Properties sysProps = System.getProperties();
//
SysProps.put("user","userid");
//
SysProps.put("password","user_password");
//
If using Sybase then DriverManager.getConnection(ConnectionString,sysProps);
}
//
打开连接
public
boolean
OpenConnection()
{
boolean
mResult
=
true
;
try
{
Class.forName(ClassString);
if
((UserName
==
null
)
&&
(PassWord
==
null
))
{
Conn
=
DriverManager.getConnection(ConnectionString);
}
else
{
Conn
=
DriverManager.getConnection(ConnectionString,UserName,PassWord);
}
Stmt
=
Conn.createStatement();
mResult
=
true
;
}
catch
(Exception e)
{
System.out.println(e.toString());
mResult
=
false
;
}
return
(mResult);
}
//
关闭数据库连接
public
void
CloseConnection()
{
try
{
Stmt.close();
Conn.close();
}
catch
(Exception e)
{
System.out.println(e.toString());
}
}
//
获取当前时间(JAVA)
public
String GetDateTime()
{
Calendar cal
=
Calendar.getInstance();
SimpleDateFormat formatter
=
new
SimpleDateFormat(
"
yyyy-MM-dd HH:mm:ss
"
);
String mDateTime
=
formatter.format(cal.getTime());
return
(mDateTime);
}
//
获取当前时间(T-SQL)
public
java.sql.Date GetDate()
{
Calendar cal
=
Calendar.getInstance();
SimpleDateFormat formatter
=
new
SimpleDateFormat(
"
yyyy-MM-dd
"
);
String mDateTime
=
formatter.format(cal.getTime());
return
(java.sql.Date.valueOf(mDateTime));
}
//
生成新的ID
public
int
GetMaxID(String vTableName,String vFieldName)
{
int
mResult
=
0
;
boolean
mConn
=
true
;
String mSql
=
new
String();
mSql
=
"
select max(
"
+
vFieldName
+
"
)+1 as MaxID from
"
+
vTableName;
try
{
if
(Conn
!=
null
)
{
mConn
=
Conn.isClosed();
}
if
(mConn)
{
OpenConnection();
}
ResultSet result
=
ExecuteQuery(mSql);
if
(result.next())
{
mResult
=
result.getInt(
"
MaxID
"
);
}
result.close();
if
(mConn)
{
CloseConnection();
}
}
catch
(Exception e)
{
System.out.println(e.toString());
}
return
(mResult);
}
//
数据检索
public
ResultSet ExecuteQuery(String SqlString)
{
ResultSet result
=
null
;
try
{
result
=
Stmt.executeQuery(SqlString);
}
catch
(Exception e)
{
System.out.println(e.toString());
}
return
(result);
}
//
数据更新(增、删、改)
public
int
ExecuteUpdate(String SqlString)
{
int
result
=
0
;
try
{
result
=
Stmt.executeUpdate(SqlString);
}
catch
(Exception e)
{
System.out.println(e.toString());
}
return
(result);
}
}
查看全文
相关阅读:
批处理向FTP上传指定属性的文件 批处理增量备份的例子
基于PHPExcel常用方法总结(phpexcel类库实例)
Expo大作战(三十四)--expo sdk api之LinearGradient(线性渐变),KeepAwake(保持屏幕不休眠),IntentLauncherAndroid,Gyroscope,
Expo大作战(三十三)--expo sdk api之MapView(地图),MailComposer(磁力传感计),Lottie(动画)
Expo大作战(三十二)--expo sdk api之Noifications
Expo大作战(三十一)--expo sdk api之Payments(expo中的支付),翻译这篇文章傻逼了,完全不符合国内用户,我只负责翻译大家可以略过!
Expo大作战(三十)--expo sdk api之Permissions(权限管理模块),Pedometer(计步器api)
生成分类编号的方法
牛腩学Kotlin做Android应用
我用Xamarin开发android应用,应用在真机上一打开就退出了
原文地址:https://www.cnblogs.com/ding0910/p/1147309.html
最新文章
大数据入门第二十五天——logstash入门
大数据入门第二十四天——SparkStreaming(二)与flume、kafka整合
ISO/IEC 9899:2011 条款6.2.3——标识符的名字空间
Unix/Linux系统下获得时间戳函数
iOS8.0如何使用Touch ID来做验证
ISO/IEC 9899:2011 条款6.2.2——标识符的连接
ISO/IEC 9899:2011 条款6.2.1——标识符的作用域
ISO/IEC 9899:2011 条款6.2——概念
ISO/IEC 9899:2011 条款6——语言
OpenGL 4.0的Tessellation Shader(细分曲面着色器)
热门文章
OS X环境下如何搭建编译Cocos2D-X v3.x的Android Studio工程
ISO/IEC 9899:2011 条款5——5.2.4 环境限制
MySQL循环语句实例教程 mysql while循环测试
mysql 数据类型TIMESTAMP用法
mysql把主键定义为自动增长标识符类型
PHP mysqli方式连接类
jstypeof方法判断undefined类型
destoon入门实例与常见问题
PHP文件锁定写入实例分享
Django在Win7下安装与创建项目hello word示例
Copyright © 2011-2022 走看看