最近感觉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();
-----------------------------------------------快乐的分割线------------------------------------------------------------------------