=================
package china;
import java.sql.*;
public class Person {
public static void main(String[] args) {
/*鏈接數據庫*/
String url="jdbc:oracle:thin:@localhost:1521:root";
String userName="SYSTEM";
String passWord="123";
Connection conn=null;
Statement stmt = null;
ResultSet res = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn=DriverManager.getConnection(url,userName,passWord);
//操作數據庫-增刪改查
//3.獲得操作數據庫聲明
Statement st=conn.createStatement();//Statement聲明 createStatement創建聲明
//4.DML 增加數據
//執行更新操作
//返回值代表該操作影響的數據記錄條數
//int i=st.executeUpdate("insert into student(sno,sname,ssex)"
//+"values('120','王五','男')");
int i=st.executeUpdate("update minzu set ceshi='考試1' where id = 12 ");
System.out.println("添加數據成功 返回值="+i);//返回值
System.out.println("連接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("連接失敗");
e.printStackTrace();
};
conn= null;
Statement st = null;
ResultSet rs = null;
}
}
查詢
package china;
import java.sql.*;
public class Person {
public static void main(String[] args) {
/*鏈接數據庫*/
String url="jdbc:oracle:thin:@localhost:1521:root";
String userName="SYSTEM";
String passWord="123";
Connection conn=null;
Statement stmt = null;
ResultSet res = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn=DriverManager.getConnection(url,userName,passWord);
//操作數據庫-增刪改查
//3.獲得操作數據庫聲明
Statement st=conn.createStatement();//Statement聲明 createStatement創建聲明
//4.DML 增加數據
//執行更新操作
//返回值代表該操作影響的數據記錄條數
//int i=st.executeUpdate("insert into student(sno,sname,ssex)"
//+"values('120','王五','男')");
// int i=st.executeUpdate("update minzu set ceshi='考試1' where id = 12 ");
ResultSet rs=st.executeQuery("select id,name,ceshi from minzu");//執行查詢,把查詢結果賦值給結果集對象
String id,name,ceshi;
while(rs.next()) {
id=rs.getString("id");//獲得id
name=rs.getString("name");//
ceshi=rs.getString("ceshi");//
System.out.println(id+"\t"+name+"\t"+ceshi);
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("連接失敗");
e.printStackTrace();
};
conn= null;
Statement st = null;
ResultSet rs = null;
}
}
