package db
import java.sql.*
public class DB {
private Connection con=null
private Statement stmt=null
private ResultSet rs=null
public DB(){}
public Connection getConnection(){
String url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"
String dbuser="root"
String dbpass="sa"
String name="com.mysql.jdbc.Driver"
if(con==null){
try{
Class.forName(name).newInstance()
}catch(Exception e){
System.out.println(e)
}
try{
con=DriverManager.getConnection(url,dbuser,dbpass)
}catch(Exception e){}
}
return con
}
public ResultSet exeQuery(String sql){
try{
con=getConnection()
if(con==null){
throw new Exception("没有可连接对象")
}
stmt=con.createStatement()
rs=stmt.executeQuery(sql)
}catch(Exception e){}
return rs
}
public boolean update(String sql){
boolean flag=true
try{
con=getConnection()
if(con==null) throw new Exception("没有可连接对象")
stmt=con.createStatement()
stmt.executeUpdate(sql)
}catch(Exception e){
flag=false
System.out.println("异常:"+e)
}
return flag
}
public void close(){
try{
if(rs!=null)try{rs.close()}catch(Exception e){System.out.println("rs"+e)}
try{stmt.close()}catch(Exception e){System.out.println("stmt"+e)}
try{con.close()}catch(Exception e){System.out.println("con"+e)}
}catch(Exception e){}
}
安装ROracle包1、安装Rtools到任意路径
2、添加r32位路径到环境变量path后。
3、添加环境变量 添加orcle的地址 如:E:\oracle\product\10.2.0\db_1\OCI\include
4、添加环境变量 添加orcle的地址 如:E:\oracle\product\10.2.0\db_1\BIN
5、安装install.packages("ROracle")包
连接句柄方式:
db_string<-("(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ip地址)(PORT =端口号))
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = crm)))")
con1<-dbConnect(drv,username="用户名",password="密码",dbname=库名)
data<-dbSendQuery(con1,"select * from user_tables")
data1<-fetch(data)
大概就这么个意思,遇到问题在百度吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)