java与数据库通过什么通信_用java方式实现与主流数据库mysql通信?

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();

}

}*/

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值