1.Java oracleԴ?源码?
Java oracleԴ??
需要用两个类来实现,DB类用来加载数据库驱动,源码建立数据库连接,源码指标源码如何获取Opdb类用来查询和插入数据
OPDB类我只写了测试用的源码main方法,需要调用DB类来做查询和插入数据的源码冬虫夏草批发溯源码操作
OPDB.java的源码如下
public class Opdb {
public static void main(String[] args) {
DB db = new DB();
String sql = "select * from user_info where user_name = '李明'";
db.doPstm(sql);//查询数据库中是否存在李明的记录
if(db.geRs()==null)//如果查询的结果为空,则插入数据{
String sql2 = "inset into user_info values("李明",源码poi3源码"男","","唱歌")";
db.doPstm(sql2);//执行插入的sql语句
}
db.close;//关闭数据库连接
}
}
DB.java的源代码如下
package com.yxq.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DB {
private Connection con;
private PreparedStatement pstm;
private String user="username";
private String password="password";
private String className="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:orcl";
public DB(){
try{
Class.forName(className);
}catch(ClassNotFoundException e){
System.out.println("加载数据库驱动失败!");
e.printStackTrace();
}
}
/**创建数据库连接*/
public Connection getCon(){
try {
con=DriverManager.getConnection(url,源码user,password);
} catch (SQLException e) {
System.out.println("创建数据库连接失败!");
con=null;
e.printStackTrace();
}
return con;
}
public void doPstm(String sql,源码Object[] params){
if(sql!=null&&!sql.equals("")){
if(params==null)
params=new Object[0];
getCon();
if(con!=null){
try{
System.out.println(sql);
pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
for(int i=0;i<params.length;i++){
pstm.setObject(i+1,params[i]);
}
pstm.execute();
}catch(SQLException e){
System.out.println("doPstm()方法出错!");
e.printStackTrace();
}
}
}
}
public ResultSet getRs() throws SQLException{
return pstm.getResultSet();
}
public int getCount() throws SQLException{
return pstm.getUpdateCount();
}
public void closed(){
try{
if(pstm!=null)
pstm.close();
}catch(SQLException e){
System.out.println("关闭pstm对象失败!源码");
e.printStackTrace();
}
try{
if(con!=null){
con.close();
}
}catch(SQLException e){
System.out.println("关闭con对象失败!源码");
源码e.printStackTrace();
源码}
源码}
源码}
源码