jdbc基本步骤:
1。注册driver
2。获取connection
url=
db name:Unix,Linux-->echo $ORACLE_SID
Win-------->net start OracleService***** “******”就是数据库名
3。创建statement
4。执行sql语句
5。如果执行的是查询,要处理结果集(resultset)
6。释放资源
1。注册driver
2。获取connection
url=
db name:Unix,Linux-->echo $ORACLE_SID
Win-------->net start OracleService***** “******”就是数据库名
3。创建statement
4。执行sql语句
5。如果执行的是查询,要处理结果集(resultset)
6。释放资源
例子:
package day1;
import oracle.jdbc.*;//导入包
import java.sql.*;
public class Jdbc2 {
public static void main(String[] args) {
Driver drv = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(drv);
conn=DriverManager.getConnection(“jdbc:oracle:thin:@192.168.0.201:1521:orcl,"softsea13","softsea13");
Statement str=con.createStatement(); //创建 Statement
String sql="insert into lwc_f(id,name) values(9527,"liangwc")";
str. executeUpdate(sql);//执行 Sql 语句
String sq2="select * from lwc_f";
ResultSet rs=str. executeQuery(String sq2);//执行 Sq2 语句,执行 select 语句后有结果集
//遍历处理结果集信息 next()如果有下一条记录返回 true,否则为 false,有,则游标向下一条记录.;
while(rs.next()){
System.out.println(rs.getInt(“id”));
System.out.println(rs.getString(“name”))
rs.close();
sta.close();
con.close();
}
}
----------------------------------------------------
还原代码:
class DriverManager{
private List<Driver> drivers=new ArrayList<Driver>;
public static void setDriver(Driver d){
drivers.add(d);
}
public static Connection getConnection(....){
for(Driver d:drivers){
if(success) return conn;
}
}
}
package day1;
import oracle.jdbc.*;//导入包
import java.sql.*;
public class Jdbc2 {
public static void main(String[] args) {
Driver drv = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(drv);
conn=DriverManager.getConnection(“jdbc:oracle:thin:@192.168.0.201:1521:orcl,"softsea13","softsea13");
Statement str=con.createStatement(); //创建 Statement
String sql="insert into lwc_f(id,name) values(9527,"liangwc")";
str. executeUpdate(sql);//执行 Sql 语句
String sq2="select * from lwc_f";
ResultSet rs=str. executeQuery(String sq2);//执行 Sq2 语句,执行 select 语句后有结果集
//遍历处理结果集信息 next()如果有下一条记录返回 true,否则为 false,有,则游标向下一条记录.;
while(rs.next()){
System.out.println(rs.getInt(“id”));
System.out.println(rs.getString(“name”))
rs.close();
sta.close();
con.close();
}
}
----------------------------------------------------
还原代码:
class DriverManager{
private List<Driver> drivers=new ArrayList<Driver>;
public static void setDriver(Driver d){
drivers.add(d);
}
public static Connection getConnection(....){
for(Driver d:drivers){
if(success) return conn;
}
}
}
class OracleDriver implements Driver{
static{
Driver d=new OracleDriver();
DriverManager.setDriver(d);
}
}
成功与否靠异常决定
------------------------------------------------
boolean execute(sql)是大而全的方法---->all sql boolean返回值表示是否有结果产生 true有 false没有
如果有 rs=stm.getResultSet()
------------------------------------------------
executeQuery(sq2);回显结果
------------------------------------------------
executeUpdate(sql);---->update insert delete 返回一个int值 int值表示影响了表里的几行。
------------------------
配置文件:xml
static{
Driver d=new OracleDriver();
DriverManager.setDriver(d);
}
}
成功与否靠异常决定
------------------------------------------------
boolean execute(sql)是大而全的方法---->all sql boolean返回值表示是否有结果产生 true有 false没有
如果有 rs=stm.getResultSet()
------------------------------------------------
executeQuery(sq2);回显结果
------------------------------------------------
executeUpdate(sql);---->update insert delete 返回一个int值 int值表示影响了表里的几行。
------------------------
配置文件:xml