以下是JdbcUtil工具类,可以调用其中的静态函数及静态语句快加载驱动,创建连接,打印结果集,关闭连接,结果集,状态。代码如下:
import java.sql.*;
public class JdbcUtil{
static{
try{
String driverName="oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
}catch(Exception e){e.printStackTrace();}
}
public static Connection getConnection(){
String url="jdbc:oracle:thin:@192.168.7.88:1521:tarena";
String username="lijp";
String pwd="lijp";
Connection con=null;
try{
con=DriverManager.getConnection(url,username,pwd);
}catch(Exception e){ e.printStackTrace();}
return con;
}
public static void printRs(ResultSet rs){
if(rs==null){
System.out.println("resultset is null");
return;
}
try{
StringBuffer sb=new StringBuffer();
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();
for(int i=1;i<=cols;i++){
sb.append(md.getColumnName(i)+"->");
sb.append(md.getColumnType(i)+"&");
sb.append(md.getColumnTypeName(i)+" "+md.getColumnDisplaySize(i)+"\n");
}
sb.append("\n\n");
while(rs.next()){
for(int i=1;i<=cols;i++){
sb.append(md.getColumnName(i)+"=");
sb.append(rs.getString(i)+" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}catch(Exception e){e.printStackTrace();}
}
public static void release(ResultSet rs,Statement stmt,Connection con){
try{
if(rs!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
try{
if(stmt!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
try{
if(con!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
}
public static void release(Object o){
try{
if(o instanceof ResultSet){
((ResultSet)o).close();
}else if(o instanceof Statement){
((Statement)o).close();
}else if(o instanceof Connection){
((Connection)o).close();
}
}catch(Exception e){e.printStackTrace();}
}
}
import java.sql.*;
public class JdbcUtil{
static{
try{
String driverName="oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
}catch(Exception e){e.printStackTrace();}
}
public static Connection getConnection(){
String url="jdbc:oracle:thin:@192.168.7.88:1521:tarena";
String username="lijp";
String pwd="lijp";
Connection con=null;
try{
con=DriverManager.getConnection(url,username,pwd);
}catch(Exception e){ e.printStackTrace();}
return con;
}
public static void printRs(ResultSet rs){
if(rs==null){
System.out.println("resultset is null");
return;
}
try{
StringBuffer sb=new StringBuffer();
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();
for(int i=1;i<=cols;i++){
sb.append(md.getColumnName(i)+"->");
sb.append(md.getColumnType(i)+"&");
sb.append(md.getColumnTypeName(i)+" "+md.getColumnDisplaySize(i)+"\n");
}
sb.append("\n\n");
while(rs.next()){
for(int i=1;i<=cols;i++){
sb.append(md.getColumnName(i)+"=");
sb.append(rs.getString(i)+" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}catch(Exception e){e.printStackTrace();}
}
public static void release(ResultSet rs,Statement stmt,Connection con){
try{
if(rs!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
try{
if(stmt!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
try{
if(con!=null)rs.close();
}catch(Exception e){ e.printStackTrace();}
}
public static void release(Object o){
try{
if(o instanceof ResultSet){
((ResultSet)o).close();
}else if(o instanceof Statement){
((Statement)o).close();
}else if(o instanceof Connection){
((Connection)o).close();
}
}catch(Exception e){e.printStackTrace();}
}
}