首页 > 代码库 > JDBC CRUD oracle

JDBC CRUD oracle

package com.org.beimei;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.org.entity.User;

public class AddTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement  pstmt = null;
		ResultSet rs = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","name","password");
			
			String sql = "insert into user(name,passoword) values(?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "name1");
			pstmt.setString(2, "pwdn1");
			
			String sql2 = "delete from user where id = ?";
			pstmt = conn.prepareStatement(sql2);
			pstmt.setInt(1, 5);

			String sql3 = "update user set name=? password=? where id=?";
			pstmt = conn.prepareStatement(sql3);
			pstmt.setString(1, "lisi");
			pstmt.setString(2, "pwd");
			pstmt.setInt(3, 5);
			
			String sql4 = "select * from user where id=1"; // 单条
			pstmt = conn.prepareStatement(sql4);
			rs = pstmt.executeQuery();
			int id = 0;
			String name ;
			String pwd ;
			if (rs.next()) {
				id = rs.getInt(1);
				name = rs.getString(2);
				pwd = rs.getString(3);
				System.out.println("姓名:"+rs.getString(2) +",密码:"+rs.getString(3) );
			}
			
			String sql5 = "select * from user";
			pstmt = conn.prepareStatement(sql5);
			rs = pstmt.executeQuery();
			List<User> list = new ArrayList<User>();
			while (rs.next()) {
				User user = new User();
				user.setId(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setPassword(rs.getString(3));
				list.add(user);
			}
			Iterator<User> iterator = list.iterator();// 放在迭代器里输出
			while (iterator.hasNext()) {
				User user = iterator.next();
				System.out.println(user.getId() + "\t" + user.getName()+ "\t"+ user.getPassword());
			}
			
			int i = pstmt.executeUpdate();
			if (i>0) {
				System.out.println("1");
			}else {
				System.err.println("0");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}



完整代码下载地址:http://download.csdn.net/detail/u013912973/7455413

简单分层代码:http://download.csdn.net/detail/u013912973/7456501