Java-JDBC连接Oracle 数据库
package com.zse.oracle; import oracle.jdbc.*; import java.sql.*; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; public class Test { public static void main(String[] args) { //载入驱动 try { Class.forName("oracle.jdbc.OracleDriver"); String url="jdbc:oracle:thin:@127.0.0.1:1521:xe"; String user="root"; String pass="111"; //通过驱动管理器获取 oracle数据库连接 Connection conn= DriverManager.getConnection(url, user, pass); //添加纪录 Statement st=conn.createStatement(); //添加语句 //String sql="insert into student values(3,'ccc','444')";//设计执行语句 //修改语句 //String sql="update student set pass='999' where sid=1"; //删除语句 //String sql="delete from student where sid=1"; //st.execute(sql);//执行语句 //读取所有记录 //String sql="select * from student where name='aaa' and pass='111' order by sid"; //通过查询返回一个结果 //模糊查询 //String sql="select count(*) as num from student where name like '%a%'"; //ResultSet rs = st.executeQuery(sql); //遍历结果输出记录 // while(rs.next())//判断是否有下一条记录存在,如果存在则游标往后移动一格 // { // //System.out.println(rs.getString("sid")+";"+rs.getString("name")+";"+rs.getString("pass")); // System.out.println(rs.getString("num")); // } //预编译:记录插入 // String sql="insert into student values(?,?,?)"; // PreparedStatement ps=conn.prepareStatement(sql); // //输入参数 // ps.setInt(1,5); // ps.setString(2, "eee"); // ps.setString(3, "888"); // // ps.execute(); //预编译:修改记录 //String sql="update student set pass=? where name=?"; //PreparedStatement ps=conn.prepareStatement(sql); //ps.setString(1, "666"); //ps.setString(2, "aaa"); //ps.execute(); //预编译:删除记录 // String sql="delete from student where sid=?"; // PreparedStatement ps=conn.prepareStatement(sql); // ps.setInt(1, 1); // ps.execute(); //预编译:查找 String sql="select * from student where name like ?"; PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, "%a%"); ResultSet rs = ps.executeQuery(); //输出结果 while(rs.next()) { System.out.println(rs.getString("sid")+";"+rs.getString("name")+";"+rs.getString("pass")); } System.out.println("记录添加成功"); //System.out.println("数据库连接成功"); // System.out.println("记录添加成功"); } catch (Exception e) { //e.printStackTrace(); System.out.println("数据库连接失败="+e.getMessage()); } } }
posted on 2016-08-04 06:37 CuriousZero 阅读(764) 评论(0) 编辑 收藏 举报