import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class Demo {
/**
* Driver用法:
* Driver driver = new oracle.jdbc.driver.OracleDriver();
* DriverManager.registerDriver(driver);
*
* Class.forName("oracle.jdbc.driver.OracleDriver");
*
* Connection用法:
* Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","system","oracle");
*
* Statement语句用法:
* Statement stmt = conn.createStatement();
* int i = stmt.executeUpdate(sql);
*
* PreparedStatement语句用法:
* PreparedStatement pstmt = conn.prepareStatement(sql);
* pstmt.setString(1,"");
* pstmt.setString(2,"");
* int i = pstmt.executeUpdate();
*
* ResultSet用法:
* ResultSet rs = stmt.executeQuery(sql);
* while(rs.next()){
* System.out.print(rs.getString(1)+"\t");
* System.out.print(rs.getString("price")+"\t");
* }
*
*/
/*新增
public static void add(){
Connection conn = null;
Statement stmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//Driver driver = new oracle.jdbc.driver.OracleDriver();
//DriverManager.registerDriver(driver);
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
stmt = conn.createStatement();
String sql = "insert into My_Price values(16,12.5,'脊骨')";
int i = stmt.executeUpdate(sql);
if(i>0){
System.out.println("数据新增成功");
}
conn.close();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
/*查询
public static void queryAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//Driver driver = new oracle.jdbc.driver.OracleDriver();
//DriverManager.registerDriver(driver);
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
stmt = conn.createStatement();
String sql = "select price_id,price,price_name from My_Price";
rs= stmt.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString("price")+"\t");
System.out.println(rs.getString("price_name"));
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
}if(stmt!=null){
stmt.close();
}if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*修改
public static void update(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//Driver driver = new oracle.jdbc.driver.OracleDriver();
//DriverManager.registerDriver(driver);
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
String sql = "update prefix_product set category=? where productid=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"未知");
pstmt.setString(2,"M08");
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("数据更新成功");
}
conn.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(pstmt!=null){
pstmt.close();
}if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*删除
public static void delete(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
//oracle.jdbc.driver.OracleDriver
//jdbc:oracle:thin:@localhost:1521:oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "oracle");
pstmt= conn.prepareStatement("delete from prefix_product where productid = ?");
pstmt.setString(1, "M09");
int i = pstmt.executeUpdate();
if(i>0){
System.out.println("受影响的行数为"+i);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//update();
//delete();
}
}