다 롄 의 --- JAVA 시스템 구조의 인사 관리 시스템

3424 단어
package com.neusoft.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcOracleTest {

	/**
	 * @param args
	 */

	public static void main(String[] args) {

		// 2.         
		// url:       
		String url = "jdbc:oracle:thin:@10.25.84.136:1521:orcl";
		String user = "scott";
		String password = "tiger";
		Connection con = null;
		Statement st = null;
		String sql = "select * from DEPT";
		String sql1 = "delete from USERTEST";
		ResultSet rs = null;
		PreparedStatement pst = null;
		// 1.    
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection(url, user, password);
			System.out.println("       !");
			//         
			con.setAutoCommit(false);

			// 3.  statement  
			st = con.createStatement();
			// 4.  statement oracle ,     
			rs = st.executeQuery(sql);
			// 5.         
			while (rs.next()) {
				System.out.println("*************************");
				String deptno = rs.getString(1);
				System.out.println(deptno);
				String dname = rs.getString(2);
				System.out.println(dname);
				String loc = rs.getString(3);
				System.out.println(loc);
				System.out.println("*************************");
			}
			// 6.    
			// int i = st.executeUpdate(sql1);
			// System.out.println("        :"+i);

			// 7.  -->?   
			pst = con.prepareStatement("insert into USERTEST values(?,?,?)");
			//       -->  serXxx()  
			pst.setString(1, "5");
			pst.setString(2, "u5");
			pst.setString(3, "p5");
			int i = pst.executeUpdate();
			System.out.println("        :" + i);
			//    -->  
			con.commit();

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				//    -->  
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		// 6.    
		try {
			if (rs != null) {
				rs.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (st != null) {
				st.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (pst != null) {
				pst.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

좋은 웹페이지 즐겨찾기