oracle数据库JDBC测试记录

来源:互联网 发布:centos git服务器配置 编辑:程序博客网 时间:2024/06/06 09:35

建表

--student表--序列DROP sequence student_seq;CREATE sequence student_seq start with 10000 increment by 1;--表结构DROP TABLE STUDENT;CREATE TABLE student(student_id NUMBER,student_name varchar2(20),student_sex char(4),student_birthday date,student_age number,student_classid number)--添加数据INSERT INTO student(student_id,student_name,student_sex,student_birthday,student_age,student_classid) VALUES(student_seq.nextval,'张三','男',TO_DATE('2014-11-11', 'yyyy-mm-dd'),10,1);SELECT* FROM student;

添加数据

package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;public class StudentProcess {    public int addStudent(String student_name,String  student_sex,String student_birthday, int  student_age,int student_classid) {        Connection connection=null;        PreparedStatement ps=null;        int n=0;        try {            //载入驱动程序            Class.forName("oracle.jdbc.driver.OracleDriver");            //获取连接            connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","dongruan","orcl");            //sql语句            String sql=" INSERT INTO student( student_id,student_name,student_sex,student_birthday,student_age,student_classid) VALUES "                    + " (student_seq.nextval , ? , ? , to_date( ?,'yyyy-mm-dd'),?,? ) ";            //创建statement对象准备执行sql语句            ps=connection.prepareStatement(sql);            ps.setString(1, student_name);            ps.setString(2, student_sex);            ps.setString(3, student_birthday);            ps.setInt(4, student_age);            ps.setInt(5, student_classid);            //执行sql语句并返回影响行数            n =ps.executeUpdate();            //影响数达到1则成功            if(n>0){                System.out.println("插入成功");            }else {                System.out.println("插入失败");            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }finally {            //关闭连接释放内存            try {                ps.close();                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        return n;    }}

查询测试

package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Jdbc2 {    public static void main(String[] args) {        Connection connection=null;        Statement st=null;        ResultSet rSet=null;        try {            //载入驱动程序            Class.forName("oracle.jdbc.driver.OracleDriver");            //获取连接            connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","orcl");            //sql语句            String sql="select * from student ";            //创建statement对象执行sql语句            st=connection.createStatement();            //执行sql语句并返回结果集            rSet=st.executeQuery(sql);            //如果有数据则获取下一条数据            while (rSet.next()) {                System.out.println("学号:"+rSet.getString("XH")+"   姓名:"+rSet.getString("XM"));            }        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }finally {            //关闭连接释放内存            try {                rSet.close();                st.close();                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}
原创粉丝点击