码迷,mamicode.com
首页 > 数据库 > 详细

java 操作oracle 建表,更新记录

时间:2014-06-27 23:26:06      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:java   get      cti      name   

1.  建立表的类

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class lx01{

 public static void main(String[] args) throws SQLException, ClassNotFoundException{
  // 加载驱动
  Class.forName("oracle.jdbc.driver.OracleDriver");
  java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  
  //建立连接
  String url = "jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
  String user="scott";
  String password="tiger";
  Connection con=java.sql.DriverManager.getConnection(url, user, password);
  
  //执行语句
  String createTableCoffees = "CREATE TABLE COFFEES " +
       "(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " +
       "SALES INTEGER, TOTAL INTEGER)";
  String createTablesup = "create table support(sup_id integer, sup_name varchar(32),city varchar(20))";
  String insertcoff="insert into coffees values(‘colombian‘,101,7.99,10,50)";
  String insertsupp="insert into support values(101,‘acme,inc.‘,‘new.york‘)";
  Statement smt=con.createStatement();
        int rs=smt.executeUpdate(insertsupp);
 }

}

2. 更新表记录的类

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class lx03{

 
  public static void main(String[] args) throws SQLException, ClassNotFoundException{
   // 加载驱动
   oracledirver();
   //建立连接
   Connection con=connect();
            Statement smt=con.createStatement();
/*            String[] sqlchar ={"insert into coffees values(‘saber tee‘,101,8.99,12,50)","insert into coffees values(‘clark tee‘,102,9.99,22,50)","insert into support values(102,‘inspur,inc.‘,‘beijing‘)"};
  for(int i=0;i<3;i++){
   smt.executeUpdate(sqlchar[i]);
  }
  */
            String sql="select b.sup_name, a.cof_name,a.price*a.sales from coffees a,support b where b.sup_id=a.sup_id";
            ResultSet rs=smt.executeQuery(sql);
            while(rs.next()){
            System.out.println(rs.getString(1)+"  "+rs.getString(2)+" "+rs.getString(3));
            }
  }
    // 加载驱动
 public static void oracledirver() throws SQLException, ClassNotFoundException{
  Class.forName("oracle.jdbc.driver.OracleDriver");
  java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
 }
 //建立连接
 public static Connection connect()throws SQLException, ClassNotFoundException{
  String url = "jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
  String user="scott";
  String password="tiger";
  Connection con=java.sql.DriverManager.getConnection(url, user, password);
  return con;
 }
/* public static String[] sql(){
  String[] sqlchar = null;
  sqlchar[0]="insert into coffees values(‘saber tee‘,101,8.99,12,50)";
  sqlchar[1]="insert into coffees values(‘clark tee‘,102,9.99,22,50)";
  sqlchar[2]="insert into support values(102,‘inspur,inc.‘,‘beijing‘)";
  return sqlchar;
 }
*/
}

 

 

java 操作oracle 建表,更新记录,布布扣,bubuko.com

java 操作oracle 建表,更新记录

标签:java   get      cti      name   

原文地址:http://blog.csdn.net/clark_xu/article/details/35285777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!