jdbc连接Oracle

最近感觉Jdbc忘得差不多了,遂拿出来温习温习。

主要代码如下:

package com.mall.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc {
	private static String url = "jdbc:oracl:thin:@localhost:1521:orcl";
	private static String username="scott";
	private static String password="123";
	public static Connection conn;
	public static PreparedStatement ps;
	public static ResultSet rs;
	public static Statement st;
	public void getConnection(){
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		Jdbc baseDao = new Jdbc();
		baseDao.getConnection();
		if(conn==null){
			System.out.println("数据库连接失败");
		}else{
			System.out.println("数据库连接成功");
		}
	}
}
首先谈谈Jdbc几个重要的对象。

Connection 

A connection (session) with a specific database. SQL statements are executed and results are returned within the context of a connection.

A Connection object's database is able to provide information describing its tables, its supported SQL grammar, its stored procedures, the capabilities of this connection, and so on. This information is obtained with the getMetaData method. 


PreparedStatement

An object that represents a precompiled SQL statement.

A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

预编译,高效

例子:

PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
                                     SET SALARY = ? WHERE ID = ?");
   pstmt.setBigDecimal(1, 153833.00)
   pstmt.setInt(2, 110592)

必须java  set类型与数据库保持一致,salary数据库中是number,id是Integer


ResultsSet

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.


A default ResultSet object is not updatable and has a cursor that moves forward only.

Thus, you can iterate through it only once and only from the first row to the last row.

上面这句话比较重要,默认是不可更新的,需要设置ResultSet的属性

关键来了

It is possible to produce ResultSet objects that are scrollable and/or updatable.

make a result set that is scrollable and insensitive to updates by others, and that is updatable.

下面的代码可以使结果集变为scrollable(可滚动的) and  insensitive(敏感的),----->然后就是updatable(应该是指update和insert)

<pre name="code" class="java">       Statement stmt = con.createStatement(
                                      ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
       ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
       // rs will be scrollable, will not show changes made by others,
       // and will be updatable

 update: 

       rs.absolute(5); // moves the cursor to the fifth row of rs
       rs.updateString("NAME", "AINSWORTH"); // updates the 
          // NAME column of row 5 to be AINSWORTH
       rs.updateRow(); // updates the row in the data source

insert:

       rs.moveToInsertRow(); // moves cursor to the insert row
       rs.updateString(1, "AINSWORTH"); // updates the 
          // first column of the insert row to be AINSWORTH
       rs.updateInt(2,35); // updates the second column to be 35
       rs.updateBoolean(3, true); // updates the third column to true
       rs.insertRow();
       rs.moveToCurrentRow();
-----------------------------------------------快乐的分割线------------------------------------------------------------------------




























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值