import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main extends JFrame{
JButton jb = new JButton();
static ResultSet rs = null;
static Statement stmt = null;
static Connection conn = null;
public Main(){
this.setTitle("Test");
this.setBounds(200, 200, 400, 200);
this.setLayout(null);
jb.setText("OK");
jb.setBounds(100, 100, 60, 30);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
call();
}
});
this.add(jb);
this.setVisible(true);
}
public void call(){
try{
String id = rs.getString("EXAMID");
String name = rs.getString("NAME");
int age = rs.getInt("AGE");
System.out.println(id + ": " + name + " " + age);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
try {
new Main();
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:XE", "sa", "sa");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("select * from SYS.STUBINFOX");
rs.next();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
上面的代码用来测试TYPE_SCROLL_SENSITIVE参数,我修改数据库以后,再点击一下按钮,可是打印出来的没有改变,不是说TYPE_SCROLL_SENSITIVE对修改是敏感的吗?昨天晚上在家测试连接ACCESS(家里机器装不了Oracle),结果恰好相反,无论是TYPE_SCROLL_SENSITIVE参数还是TYPE_SCROLL_INSENSITIVE都敏感,只要数据库一修改,打印的结果马上就跟着修改。完全没有头绪,请教给予支持!!!谢谢!!!!!!
------解决方案--------------------
http://blog.csdn.net/axman/archive/2009/03/12/3984103.aspx
------解决方案--------------------
2.TYPE_SCROLL_INSENSITIVE,双向滚动,但不及时更新,就是如果数据库里的数据修改过,并不在ResultSet中反应出来。
3.TYPE_SCROLL_SENSITIVE,双向滚动,并及时跟踪数据库的更新,以便更改ResultSet中的数据
http://blog.csdn.net/axman/archive/2009/03/12/3984103.aspx
这个问题我在几年前说过,但今天再次从CSDN上看到有人问这个问题,可以看出,真正懂这个问题的人1%都不到。
我再次把这个问题写在这里,希望光临我的BLOG的人能真正了解它。
我们先来做一个例子,在例子中我用的是mysql-essential-5.1.30-win32版。
来跟我做以下几个命令:
mysql> create database axman;
mysql> use axman;
mysql> create table axmantest(
-> id int(4) not null auto_increment primary key
-> name varchar(20));
mysql> insert into axmantest (name) values ('axman')
mysql> insert into axmantest (name) values ('sager')
mysql> insert into axmantest (name) values ('p4');
OK,写一个测试程序:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainTest {
public static void main(String[] args) throws Exception{
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/axman?useUnicode=true&characterEncoding=UTF-8","root","password");
Statement stmt = conn.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
ResultSet rs = stmt.executeQuery("select * from axmantest");