JAVA JDBC六步

package com.jdbcs.test01;
import com.sun.deploy.nativesandbox.comm.Response;
import sun.misc.Request;
import java.io.PrintWriter;
import java.sql.*;
public class test01 {
public static void main(String[] args) throws SQLException {
Statement sta = null;
Connection conn=null;
ResultSet r =null;
Response response;
try {
//1.用反射的方法获取驱动 Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获取数据库连接(数据库官方提供的url,用户名,密码)
conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","juzc","123");
//3.获取数据库操作对象
sta=conn.createStatement();
//4.进行sql语句编写
//4.1插入语句
String str = "insert into user1(user_id,user_name,password)values(seq_user.nextval,'cs','333')";
//执行更新操作
sta.executeUpdate(str);
//4.2查询语句
String str="select user_name,password from user1";
r=sta.executeQuery(str);
while(r.next()){
System.err.println(r.getString("user_name")+ r.getString("password"));
}
//5.如果是查询要对结果集进行处理
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//6.关闭资源
sta.close();
conn.close();
}
}
}





