日期:2014-05-16 浏览次数:20429 次
oracle脚本:drop table t_student cascade constraints;
/*==============================================================*/ /* Table: t_student */ /*==============================================================*/ create table t_student ( s_id char(10) not null, s_name varchar2(20) not null, s_age char(2) not null, s_sex char(2) not null, s_class varchar2(20), constraint PK_T_STUDENT primary key (s_id) ); insert into t_student values('0001','张三','20','男','08级二班') ; insert into t_student values('0002','李四','21','女','08级二班') ; insert into t_student values('0003','王五','20','男','08级二班') ; insert into t_student values('0004','赵柳','20','女','08级一班') ; insert into t_student values('0005','杨梅','21','男','08级二班') ; insert into t_student values('0006','刘海','23','女','08级一班') ; insert into t_student values('0007','孙江','20','女','08级一班') ; insert into t_student values('0008','苏灿','22','男','08级二班') ; insert into t_student values('0009','王霞','23','女','08级一班') ; insert into t_student values('0010','王猛','22','男','08级二班') ; insert into t_student values('0011','张相','22','女','08级一班') ; insert into t_student values('0012','香橙','20','女','08级一班') ; insert into t_student values('0013','李心','21','女','08级二班') ; insert into t_student values('0014','张强','20','男','08级一班') ; insert into t_student values('0015','赵琳','21','女','08级一班') ; insert into t_student values('0016','刘达','21','男','08级二班') ; insert into t_student values('0017','苏惠','20','女','08级二班') ; insert into t_student values('0018','贾瑞','20','女','08级一班') ; insert into t_student values('0019','谷瑞坤','22','男','08级二班') ; insert into t_student values('0020','祥还','21','男','08级一班') ; commit;
??采用单利模式创建DbUtil类获得Connection对象:package com.stmcc.test.util;
import java.sql.*; public class DbUtil { private static final String driver = "oracle.jdbc.driver.OracleDriver" ; private static final String url = "jdbc:oracle:thin:@10.10.10.2:1521:orcl" ; private static final String username = "test" ; private static final String password = "test" ; public static Connection getConnection(){ Connection conn = null ; try{ Class.forName(driver) ; conn = DriverManager.getConnection(url, username, password) ; }catch(Exception e){ e.printStackTrace() ; } return conn ; } public static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(PreparedStatement pstmt) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs ) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { System.out.println(DbUtil.getConnection()); } }
?创建学生实体类:package com.stmcc.test;
public class Student { private String s_id ; private String s_name ; private String s_age ; private String s_sex ; private String s_class ; public String getS_id() { return s_id; } public void setS_id(String s_id) { this.s_id = s_id; } public String getS_name() { return s_name; } public void setS_name(String s_name) { this.s_name = s_name; } public String getS_age() { return s_age; } public void setS_age(String s_age) { this.s_age = s_age; } public String getS_sex() { return s_sex; } public void setS_sex(String s_sex) { this.s_sex = s_sex; } public String getS_class() { return s_class; } public void setS_class(String s_clas