国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java語言數(shù)據(jù)庫操作的基本流程

2019-11-17 05:53:30
字體:
供稿:網(wǎng)友
1.java數(shù)據(jù)庫操作基本流程
a .數(shù)據(jù)庫連接1.Drivermanager 鏈接數(shù)據(jù)庫
String className,url,uid,pwd;
className="Oracle.jdbc.driver.OracleDriver";
uid="scott";
pwd="tiger";
url="jdbc:oracle:thin:@localhost:1521:ora92";
Class.forName(classname);
Connection conn=DriverManager.getConnection(url,uid,pwd);
2.JNDI鏈接數(shù)據(jù)庫
String jndi ="jdbc/db"; //  e20-040 9L0-609 數(shù)據(jù)源的名稱
//context是一組名稱到對象的綁定組成
Hashtable env=new Hashtable ();
Context ctx=(Context)new InitialContext.lookup("env");// 獲得數(shù)據(jù)源所在的上下文點(diǎn)的對象
DataSource ds=(DataSource)ctx.lookup(jndi);//找到數(shù)據(jù)源


Connection conn=ds.getConnection();//
b.執(zhí)行 sql語句
String sql;
StateMent stat=conn.createStatement();
ResultSet rs=stat.executeQuery(sql);//執(zhí)行數(shù)據(jù)的查詢語句(select);
stat.executeUpdate(sql);//執(zhí)行數(shù)據(jù)的更新語句(inset into ,delete ,update ,drop)
stat.close();
c.用PReparedStatement 來執(zhí)行sql語句
String sql="inset into table(id,name) values(?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,001);
ps.setString(2,"zhangmanli");

ps.executeQuery();
int count=ps.executeUpdate();
d.處理執(zhí)行結(jié)果
查詢語句,返回記錄集ResultSet對象
更新語句,返回數(shù)字,表示該更新影響的記錄數(shù)
javax.sql.*
javax.naming.*;
數(shù)據(jù)處理:
1關(guān)閉connection 的自動提交
conn.setAutoCommit(false);
2執(zhí)行一系列sql 語句,
Statement sm;
sm=conn.createStatement(sql);
sm.executeUpdate();
sm.close();

3.提交:
conn.commit();
4.回滾機(jī)制;
conn.rollback();
e:線程處理:
D:jndi和dataSource 來獲得數(shù)據(jù)庫的鏈接:
import java.sql.ResultSet ;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
public class BasicExample{
 public static void main(String args[]){
  Connection conn=null;
  try{
   Properties prop =new Properties();
   prop.load(new FileInputStream("simple.properties"));
   Hashtable env =new Hashtable();
   env.put(Context.INITIAL_CONTEXT_FACTORY,prop.getProperty("INITIAL_CONTEXT_FACTORY"));
   env.put(Context.PROVIDER_URL,prop.getProperty("PROVIDER_URL"));
   InitialContext ctx=new InitialContext(env);
  DataSource ds=(DataSource)ctx.lookup("Book");


   Conn=ds.getConnection();
   Statement stat=conn.createStatement();;
   ResultSet rs=stmt.executeQuery(sql);
   while(rs.next()){
    int id=Integer.parseInt(rs.getString("userId"));
    String userName=rs.getString ("username");

   }
  }catch(SQLException e){
   e.printStackTrace();
  }finally{
   try{
    if(conn!=null){
     conn.close();
    }
   }catch(SQLException e){
    e.printStackTrace();
   }
  }
 }
};



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 项城市| 绥阳县| 法库县| 德州市| 临高县| 黄浦区| 裕民县| 永靖县| 建始县| 庐江县| 象州县| 福泉市| 正阳县| 格尔木市| 疏勒县| 喜德县| 嘉荫县| 谢通门县| 平果县| 阜新市| 阳新县| 兴文县| 沧州市| 衡阳市| 疏勒县| 岗巴县| 宁陕县| 江北区| 兴山县| 石棉县| 静乐县| 滨海县| 余干县| 双城市| 泾源县| 玉山县| 平和县| 河池市| 武城县| 孟津县| 中超|