以下是我写的代码
public class MyJdbc {
private String host; //本地主机
private String port; //端口
private String sid; //主机名
private String user; //用户名
private String password; //密码
private StringBuffer url = new StringBuffer("jdbc:oracle:thin:@"); //连接字符串
private Connection con;
MyJdbc(String host,String port,String sid,String user,String password){
this.host = host;
this.port = port;
this.sid = sid;
this.user = user;
this.password = password;
this.url.append(this.host).append(":").append(this.port).append(":").append(this.sid);
//System.out.println(url.toString());
}
void connect() //注册驱动和连接数据库
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("注册成功");
} catch (ClassNotFoundException e) {
System.out.println("注册失败");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url.toString(),user,password);
System.out.println("连接成功");
} catch (SQLException e) {
System.out.println("连接失败");
e.printStackTrace();
}
}
void showTbale(String sql) //显示表的信息
{
Statement st = null;
ResultSet rs = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = st.executeQuery(sql);
ResultSetMetaData rsm = rs.getMetaData();
int i = rsm.getColumnCount();
while(rs.next()){
for(int j = 1 ; j <= i ; j++){
System.out.print(rs.getString(j)+" ");
}
System.out.println("");
};
rs.close();
st.close();
} catch (SQLException e) {
System.out.println("表视图不存在");
e.printStackTrace();
}
finally{
}
}
void operateTable(String sql) //对表的操作例如 建表,插入,删除,更新。
{
Statement st = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.executeUpdate(sql);
System.out.println("操作成功");
st.close();
} catch (SQLException e) {
System.out.println("表或视图不存在或无效SQL语句");
}
}
final void close(){
try {
con.close();
System.out.println("已关闭连接");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
为什么我这只有插入信息的时候才会成功~其他例如操作删除,更新没用~好象显示也不能立即更新~哪个帮忙解决下~~~~
public class MyJdbc {
private String host; //本地主机
private String port; //端口
private String sid; //主机名
private String user; //用户名
private String password; //密码
private StringBuffer url = new StringBuffer("jdbc:oracle:thin:@"); //连接字符串
private Connection con;
MyJdbc(String host,String port,String sid,String user,String password){
this.host = host;
this.port = port;
this.sid = sid;
this.user = user;
this.password = password;
this.url.append(this.host).append(":").append(this.port).append(":").append(this.sid);
//System.out.println(url.toString());
}
void connect() //注册驱动和连接数据库
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("注册成功");
} catch (ClassNotFoundException e) {
System.out.println("注册失败");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url.toString(),user,password);
System.out.println("连接成功");
} catch (SQLException e) {
System.out.println("连接失败");
e.printStackTrace();
}
}
void showTbale(String sql) //显示表的信息
{
Statement st = null;
ResultSet rs = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = st.executeQuery(sql);
ResultSetMetaData rsm = rs.getMetaData();
int i = rsm.getColumnCount();
while(rs.next()){
for(int j = 1 ; j <= i ; j++){
System.out.print(rs.getString(j)+" ");
}
System.out.println("");
};
rs.close();
st.close();
} catch (SQLException e) {
System.out.println("表视图不存在");
e.printStackTrace();
}
finally{
}
}
void operateTable(String sql) //对表的操作例如 建表,插入,删除,更新。
{
Statement st = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.executeUpdate(sql);
System.out.println("操作成功");
st.close();
} catch (SQLException e) {
System.out.println("表或视图不存在或无效SQL语句");
}
}
final void close(){
try {
con.close();
System.out.println("已关闭连接");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
为什么我这只有插入信息的时候才会成功~其他例如操作删除,更新没用~好象显示也不能立即更新~哪个帮忙解决下~~~~
解决方案 »
- java如何打开一个带有IP地址的文件
- Java gui编程中遇到的问题 java.lang.NullPointerException
- 怎样在eclipse下安装jasperreport插件
- 求助关于FileOutputStream的write方法?
- 直线与多边形相交的问题!
- 请问,将mysql打入可执行jar包的方法
- hashtable中的数据怎么按value值的大小(int型的)排列啊?然后存到另一个数据结构中?
- 我用JAVAC编译一个目录,javac C:\src\*.java -d C:\classes,importJAVA自已如java.awt.*没问题,import 这个目录下其它类就出问题
- 如何求File的大小???高分相送!!
- 有没有过来人谈谈SL-275考试?
- List状态的问题
- JTextPane显示水平滚动条问题
int executeUpdate(String sql)
throws SQLExceptionExecutes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters:
sql - an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing
Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
Throws:
SQLException - if a database access error occurs or the given SQL statement produces a ResultSet object