关于Oracle的在Eclipse中操作的命令语句


Connection con=this.creatCon();//获得连接
   PreparedStatement pstm=null;//命令器

String sql="insert into student(student_id,name,sex,age) values(?,?,?,?)";//设置要执行的SQL语句
    pstm=con.prepareStatement(sql);//准备执行SQL语句命令
    pstm.setString(2, name);//设置第2个问号的值
    pstm.setString(3, sex);//设置第3个问号的值
    pstm.setInt(4, age);//设置第4个问号的值
    pstm.setInt(1,student_id);//设置第1个问号的值
    //pstm.executeQuery();//执行SQL命令,这个命令是在查询的时候,返回数据集ResultSet
    pstm.executeUpdate();//执行SQL命令,当是插入,删除,修改的时候用
    System.out.println("写入成功");//执行后写出是否成功

连接的打开和关闭:
// 创建一个连接Oracle数据库的类
public static Connection creatCon()
{
   String username="scott";
   String password="tiger";
   Connection con=null;
   try
   {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url="java:oracle:thin:@localhost:1521:ora9i";//连接Oracle数据库

     /*class.forName("com.micrsoft.jdbc.sqlserver.SQLServerDriver");
     String url="java:microsoft:sqlserver://localhost:1433";//连接SQL server数据库*/


    con=(Connection)DriverManager.getConnection(url,username,password);
    return con;
   }
   catch(Exception exc)
   {
    System.out.println(exc);
   }
   return null;
}
//关闭连接
public void closeCon(Connection c)
{
   try
   {
    c.close();
   }
   catch(Exception c1)
   {
    System.out.println(c1);
   }
}

相关内容