---
title: "OracleText"
author: "kkllmey"
date: "2008-10-14T16:46:46Z"
tags: []
slug: "oracletext"
draft: false
lastmod: "2008-10-14T16:46:46Z"
id: 252758
url: "https://ibc.iteye.com/blog/252758"
---

```
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();
			}
		}

	}
}
```