자바 데이터베이스 프로 그래 밍 의 일부 코드
                                            
 3424 단어  it
                    
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.85.247: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();
		}
	}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
LITTLE SHOP OF FLOWERS(DP)You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. These id-num...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.