日期:2014-05-20 浏览次数:20705 次
create table users
(id int primary key,
name varchar2(30) not null,
password varchar2(32) not null,
age int not null,
sex varchar2(2) not null);
insert into users values(1, '李兴华', 'www.mldn.cn', 30, '男');
insert into users values(2, '李康', 'www.mldnjava.cn', 23, '女');
package org.lxh.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class InsertDemo1 {
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
public static final String DBUSER = "scott";
public static final String DBPASSWORD = "wang913292836";
public static void main(String args[]) throws Exception {
Connection conn = null; //数据库连接
PreparedStatement pstmt = null; //数据库操作
ResultSet rs = null; //保存查询结果
String sql = "select id, name, password, age, sex" +
"from users";
Class.forName(DBDRIVER);//加载驱动程序
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
pstmt = conn.prepareStatement(sql); //实例化PreparedStatement对象
rs = pstmt.executeQuery(); //实例化ResultSet对象
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int age = rs.getInt(4);
String sex = rs.getString(5);
System.out.print("编号:" + id + "; ");
System.out.print("姓名:" + name + "; ");
System.out.print("密码:" + password + "; ");
System.out.print("年龄:" + age + "; ");
System.out.println("性别:" + sex + "; ");
}
rs.close(); //关闭结果集