代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java 连接各种数据库的函数
作者:
/ 发布于
2011/1/14
/
532
<div><span style="color: #008000">/*</span><span style="color: #008000"> * Db.java Created on 2007年8月20日, 上午 8:37 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.io.</span><span style="color: #000000">*</span><span style="color: #000000">; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.</span><span style="color: #000000">*</span><span style="color: #000000">; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Properties; </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> Db { </span><span style="color: #0000ff">private</span><span style="color: #000000"> String driver; </span><span style="color: #0000ff">private</span><span style="color: #000000"> String url; </span><span style="color: #0000ff">private</span><span style="color: #000000"> String user; </span><span style="color: #0000ff">private</span><span style="color: #000000"> String password; </span><span style="color: #0000ff">private</span><span style="color: #000000"> Connection conn; </span><span style="color: #0000ff">private</span><span style="color: #000000"> Statement stm; </span><span style="color: #0000ff">private</span><span style="color: #000000"> ResultSet rs; </span><span style="color: #0000ff">public</span><span style="color: #000000"> Db(){ </span><span style="color: #0000ff">this</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">DBConf.properties</span><span style="color: #000000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">public</span><span style="color: #000000"> Db(String conf) { loadProperties(conf); setConn(); } </span><span style="color: #0000ff">public</span><span style="color: #000000"> Connection getConn(){ </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">this</span><span style="color: #000000">.conn; } </span><span style="color: #008000">//</span><span style="color: #008000">handle the properties file to get the informations for connection</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> loadProperties(String conf){ Properties props </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Properties(); </span><span style="color: #0000ff">try</span><span style="color: #000000"> { props.load(</span><span style="color: #0000ff">new</span><span style="color: #000000"> FileInputStream(conf)); } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (FileNotFoundException e) { e.printStackTrace(); } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (IOException e) { e.printStackTrace(); } </span><span style="color: #0000ff">this</span><span style="color: #000000">.driver </span><span style="color: #000000">=</span><span style="color: #000000"> props.getProperty(</span><span style="color: #000000">"</span><span style="color: #000000">driver</span><span style="color: #000000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">this</span><span style="color: #000000">.url </span><span style="color: #000000">=</span><span style="color: #000000"> props.getProperty(</span><span style="color: #000000">"</span><span style="color: #000000">url</span><span style="color: #000000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">this</span><span style="color: #000000">.user </span><span style="color: #000000">=</span><span style="color: #000000"> props.getProperty(</span><span style="color: #000000">"</span><span style="color: #000000">user</span><span style="color: #000000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">this</span><span style="color: #000000">.password </span><span style="color: #000000">=</span><span style="color: #000000"> props.getProperty(</span><span style="color: #000000">"</span><span style="color: #000000">password</span><span style="color: #000000">"</span><span style="color: #000000">); } </span><span style="color: #008000">//</span><span style="color: #008000">implement the Connection</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> setConn(){ </span><span style="color: #0000ff">try</span><span style="color: #000000"> { Class.forName(driver); </span><span style="color: #0000ff">this</span><span style="color: #000000">.conn </span><span style="color: #000000">=</span><span style="color: #000000"> DriverManager.getConnection(url,user,password); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(ClassNotFoundException classnotfoundexception) { classnotfoundexception.printStackTrace(); System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db: </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> classnotfoundexception.getMessage()); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException sqlexception) { System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db.getconn(): </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> sqlexception.getMessage()); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> doInsert(String sql) { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { Statement statement </span><span style="color: #000000">=</span><span style="color: #000000"> conn.createStatement(); </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> stm.executeUpdate(sql); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException sqlexception) { System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db.executeInset:</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> sqlexception.getMessage()); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> doDelete(String sql) { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { stm </span><span style="color: #000000">=</span><span style="color: #000000"> conn.createStatement(); </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> stm.executeUpdate(sql); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException sqlexception) { System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db.executeDelete:</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> sqlexception.getMessage()); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> doUpdate(String sql) { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { stm </span><span style="color: #000000">=</span><span style="color: #000000"> conn.createStatement(); </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> stm.executeUpdate(sql); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException sqlexception) { System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db.executeUpdate:</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> sqlexception.getMessage()); } } </span><span style="color: #0000ff">public</span><span style="color: #000000"> ResultSet doSelect(String sql) { </span><span style="color: #0000ff">try</span><span style="color: #000000"> { stm </span><span style="color: #000000">=</span><span style="color: #000000"> conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); rs </span><span style="color: #000000">=</span><span style="color: #000000"> stm.executeQuery(sql); } </span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException sqlexception) { System.err.println(</span><span style="color: #000000">"</span><span style="color: #000000">db.executeQuery: </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> sqlexception.getMessage()); } </span><span style="color: #0000ff">return</span><span style="color: #000000"> rs; } </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args){ </span><span style="color: #0000ff">try</span><span style="color: #000000">{ Db db </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Db(); Connection conn </span><span style="color: #000000">=</span><span style="color: #000000"> db.getConn(); </span><span style="color: #0000ff">if</span><span style="color: #000000">(conn </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> </span><span style="color: #000000">!</span><span style="color: #000000">conn.isClosed()) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">連結成功</span><span style="color: #000000">"</span><span style="color: #000000">); ResultSet rs </span><span style="color: #000000">=</span><span style="color: #000000"> db.doSelect(</span><span style="color: #000000">"</span><span style="color: #000000">select * from content</span><span style="color: #000000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">while</span><span style="color: #000000">(rs.next()){ System.out.println(rs.getString(</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">+</span><span style="color: #000000">"</span><span style="color: #000000">:</span><span style="color: #000000">"</span><span style="color: #000000">+</span><span style="color: #000000">rs.getString(</span><span style="color: #000000">2</span><span style="color: #000000">)</span><span style="color: #000000">+</span><span style="color: #000000">"</span><span style="color: #000000">:</span><span style="color: #000000">"</span><span style="color: #000000">+</span><span style="color: #000000">rs.getString(</span><span style="color: #000000">3</span><span style="color: #000000">)); } rs.close(); conn.close(); } }</span><span style="color: #0000ff">catch</span><span style="color: #000000">(SQLException e) { e.printStackTrace(); } } } </span></div>
试试其它关键字
连数据库
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3