package com.hd.jdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.RowSet;
import sun.jdbc.rowset.CachedRowSet;
public class Orcalzx {
private static String urls,users,passwords;
static
{
/* Properties prop=new Properties();
urls="jdbc:mysql://localhost:3306/invest?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false";
users="root";
passwords="123";
}
public static Connection getConnection() {
Connection con = null;
try {
//con = DriverManager.getConnection(url, user, password);
con=locateDs().getConnection(); //通过数据源得到连接
} catch (SQLException e) {
System.out.println(e.toString());
}
return con;
}
public static DataSource locateDs()
{
DataSource ds = null;
HashMap cachedDs = new HashMap();
if (cachedDs.containsKey("ds"))
ds = (DataSource)cachedDs.get("ds");
else {
try {
//初始化上下文
Context initCtx = new InitialContext();
ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/ds"); //jdbc/ds是数据源名称
//ds = (DataSource)initCtx.lookup("java:/ds"); //jboss数据源连接方式
cachedDs.put("ds", ds);
} catch (Exception e) {
e.printStackTrace();
}
}
return ds;
}
public static RowSet query(String sql)
{
CachedRowSet crs=null;//需要选择sun.jdbc.rowset.CachedRowSet;
ResultSet rs=null;
Connection conn=getConnection();
try {
crs=new CachedRowSet();
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
crs.populate(rs);
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return crs;
}
/*public static boolean preupdate(String sql)
{
boolean f=true;
Connection conn=getConnection();
try {
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1, 6);
stmt.setString(2, "gfkg");
stmt.setString(3, "gflg");
//System.out.println(stmt.executeUpdate());
//stmt.executeUpdate(sql);
} catch (SQLException e) {
f=false;
e.printStackTrace();
}
finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return f;
}*/
public static boolean update(String sql)
{
boolean f=true;
Connection conn=getConnection();
try {
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
f=false;
//e.printStackTrace();
}
finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return f;
}
/* public static String select(String sql){
String pwd="";
try {
ResultSet rs=query(sql);//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1); //列的索引
//user=rs.getString("username");
pwd=rs.getString("dept");
System.out.println(pwd);
//System.out.println(" /密码"+password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pwd;
}
public static void main(String[] args) {
// System.out.println(Orcalzx.getConnection());
String sql="select * from jtest where rownum<10";
try {
ResultSet rs=query(sql);//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1); //列的索引
int id=rs.getInt("id");
user=rs.getString("username");
password=rs.getString("password");
System.out.println("id:"+id+" /用户"+user+" /密码"+password);
}
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select * from users";
try {
ResultSet rs=query(sql);//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1); //列的索引
user=rs.getString("username");
password=rs.getString("pwdhash");
System.out.println(" /用户"+user+" /密码"+password);
}
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select * from (select rownum rid, t.* from employee t where rownum<=3)t where rid>0";
//query(sql);
ResultSet rs=query(sql);//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
try {
while(rs.next())
{
//int id=rs.getInt(1); //列的索引
//user=rs.getString("username");
String pwd=rs.getString("FIRST_NAME");
System.out.println(pwd);
//System.out.println(" /密码"+password);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}