欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OracleText 博客分类: Oracle OracleJDBCSQL 

程序员文章站 2024-02-14 19:15:40
...
package com.i18n;

import java.sql.*;

import javax.transaction.Transaction;

public class OracleTest {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:ora10";
		String driver = "oracle.jdbc.driver.OracleDriver";
		Connection con = null;
		//
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url,"system","abc");
			Statement st =  con.createStatement();
			ResultSet rs= st.executeQuery("select * from stu");
			while(rs.next()){
				System.out.println(rs.getString("username"));
			}
			st.close();
			con.setAutoCommit(false); //自动提交事务为false
			PreparedStatement ps = con.prepareStatement("insert into stu values('dddd')");
			ps.executeUpdate("insert into stu values('zhi')");
			con.commit(); //提交事务
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback(); //回事务
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}finally{
			try{
			if(null!=con)
				con.close();
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}

	}
}
 
相关标签: Oracle JDBC SQL