import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
/**
* @param args
*/
static Connection conn = null;
static PreparedStatement ps = null;
static int rs ;
static ResultSet rst = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
//update();
//insert();
//find();
delete();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
// if(rs!=null){
// rs.close();
// }
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查
public static void find(){
//String sql = "select staffname,staff_id from staff123 where mobile = ?";
String sql = "select staffname,staff_id from staff123";
try {
ps = conn.prepareStatement(sql);
//ps.setString(1, "187****1533");
rst = ps.executeQuery();
while(rst.next()){
System.out.println("姓名:"+rst.getString("staffname")+",工号:"+rst.getString("staff_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//改
public static void update(){
String update = "update tabccarinsurance t set t.syx_cost = 100 where t.carno = ?";
try {
ps = conn.prepareStatement(update);
ps.setString(1, "冀A00333");
rs = ps.executeUpdate();
System.out.println("車牌:"+"carno"+",交強險:"+"syx_cost");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增
public static void insert(){
String sql = "insert into staff123(staff_id,staffname,mobile) values(?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, "12302");
ps.setString(2, "舒大");
ps.setString(3, "173****6891");
rs = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//刪
public static void delete(){
String sql = "delete from staff123 where mobile = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,"187****1533");
rs = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
/**
* @param args
*/
static Connection conn = null;
static PreparedStatement ps = null;
static int rs ;
static ResultSet rst = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@地址:端口:数据库","用户名","密码");
//在数据库的安装目录下D:\app\xiongjt\product\11.2.0\dbhome_1\NETWORK\ADMIN的tnsnames.ora文件中
//AAAA =
// (DESCRIPTION =
// (ADDRESS_LIST =
// (ADDRESS = (PROTOCOL = TCP)(HOST = 地址)(PORT = 端口))
// )
// (CONNECT_DATA =
// (SERVER = dedicated)
// (SERVICE_NAME = 数据库)
// )
// )
//update();
//insert();
//find();
delete();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
// if(rs!=null){
// rs.close();
// }
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查
public static void find(){
//String sql = "select staffname,staff_id from staff123 where mobile = ?";
String sql = "select staffname,staff_id from staff123";
try {
ps = conn.prepareStatement(sql);
//ps.setString(1, "187****1533");
rst = ps.executeQuery();
while(rst.next()){
System.out.println("姓名:"+rst.getString("staffname")+",工号:"+rst.getString("staff_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//改
public static void update(){
String update = "update tabccarinsurance t set t.syx_cost = 100 where t.carno = ?";
try {
ps = conn.prepareStatement(update);
ps.setString(1, "冀A00333");
rs = ps.executeUpdate();
System.out.println("車牌:"+"carno"+",交強險:"+"syx_cost");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增
public static void insert(){
String sql = "insert into staff123(staff_id,staffname,mobile) values(?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, "12302");
ps.setString(2, "舒大");
ps.setString(3, "173****6891");
rs = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//刪
public static void delete(){
String sql = "delete from staff123 where mobile = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,"187****1533");
rs = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}