package com.tarena.jdbclab;
import java.sql.*;
public class Lab3{
public static void main(String[] args){
Connection con = null;
Statement stmt = null;
String driverName ="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.0.23:1521:tarena";
String usr = "openlab";
String pwd = "open123";
try{
Class.forName(driverName);
con = DriverManager.getConnection(url,usr,pwd);
System.out.println("con->"+con);
stmt = con.createStatement();
System.out.println("stmt->"+stmt);
String sql = "drop table "+args[0];
int num = -1;
try{
num = stmt.executeUpdate(sql);
System.out.println("drop table "+num);
}catch(Exception ex){
System.out.println("表"+args[0]+"不存在");
}
sql ="create table "+args[0]+"(id number(8) primary key,"+"name varchar(15))";
num = stmt.executeUpdate(sql);
System.out.println("table "+
args[0]+" created! "+num);
sql = "insert into "+args[0]+" values(1,'Mike')";
num = stmt.executeUpdate(sql);
System.out.println("insert "+num+" record");
}catch(Exception e){
e.printStackTrace();
}finally{
//6.关闭JDBC对象
try{
if(stmt!=null) stmt.close();
}catch(Exception ex){
ex.printStackTrace();
}
try{
if(con!=null) con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
int num=-1 有什么用,数字是不是可以任意定义
打印语句中的num结果不太明白
哪位高人讲解下
import java.sql.*;
public class Lab3{
public static void main(String[] args){
Connection con = null;
Statement stmt = null;
String driverName ="oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.0.23:1521:tarena";
String usr = "openlab";
String pwd = "open123";
try{
Class.forName(driverName);
con = DriverManager.getConnection(url,usr,pwd);
System.out.println("con->"+con);
stmt = con.createStatement();
System.out.println("stmt->"+stmt);
String sql = "drop table "+args[0];
int num = -1;
try{
num = stmt.executeUpdate(sql);
System.out.println("drop table "+num);
}catch(Exception ex){
System.out.println("表"+args[0]+"不存在");
}
sql ="create table "+args[0]+"(id number(8) primary key,"+"name varchar(15))";
num = stmt.executeUpdate(sql);
System.out.println("table "+
args[0]+" created! "+num);
sql = "insert into "+args[0]+" values(1,'Mike')";
num = stmt.executeUpdate(sql);
System.out.println("insert "+num+" record");
}catch(Exception e){
e.printStackTrace();
}finally{
//6.关闭JDBC对象
try{
if(stmt!=null) stmt.close();
}catch(Exception ex){
ex.printStackTrace();
}
try{
if(con!=null) con.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
int num=-1 有什么用,数字是不是可以任意定义
打印语句中的num结果不太明白
哪位高人讲解下
INSERT、UPDATE 或 DELETE 语句的行计数;或者 0,表示不返回任何内容的 SQL 语句 即num保存的是执行stmt.executeUpdate(sql); 后影响的行数!
try{
num = stmt.executeUpdate(sql);
System.out.println("drop table "+num);
}catch(Exception ex){
System.out.println("表"+args[0]+"不存在");
} 当然可以随意定义...
num = stmt.executeUpdate(sql); 返回的是操作所影响的记录数.
比入插入了一条记录,返回1 NUM值就是1了.
如果SQL语句是删除了3条记录,NUM的值就是3了.
如果是创建表等语句,则返回0这里设为-1是因为,如果SQL语句执行失败的话,NUM的值就是-1噻..一般在代码规范中可以表示失败.
比字符串查找" ssssss".indexOf("a") 如果未找到也是返回-1 这不是关键的,只是一个标记而已..