[备忘] java jdbc odbc 连接oracle

 
import java.sql.*;

public class Demo {

	public static void main(String[] args) {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//找到这个类并返回这个类
		} catch (ClassNotFoundException e) {
			System.out.println("the driver is not found");
			e.printStackTrace();
			return;
		}
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		try {
			// conn = DriverManager.getConnection("jdbc:odbc:hr","hr","hr");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");

			st = conn.createStatement();
			rs = st.executeQuery("select * from jobs");
			rsmd = rs.getMetaData();// 获取元数据
			// 此处可添加对数据库中表的相关信息的获取,比如列名,列数等
			while (rs.next()) {
				System.out.print(rs.getString(1) + "\t");
				System.out.print(rs.getString(2) + "\t");
				System.out.print(rs.getString(3) + "\t");
				System.out.println(rs.getString(4) + "\t");

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				st.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

你可能感兴趣的:([备忘] java jdbc odbc 连接oracle)