=================
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; } }