Java JDBC连接Oracle,java面试题,java初级笔试题


写在最前面,我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家。扫码加微信好友进【程序员面试学习交流群】,免费领取。也欢迎各位一起在群里探讨技术。

 

1. 安装Oracle数据库,我这里使用的是Oracle 12c

 

2. 创建Java工程 connection-oracle

Java JDBC连接Oracle,java面试题,java初级笔试题_第1张图片

注意:使用的JavaSE-1.8

3. 在Oracle的安装目录里,将djdbc8.jar 拷贝到java工程

路径为:C:apporacleproduct .2.0dbhome_1jdbclib

Java JDBC连接Oracle,java面试题,java初级笔试题_第2张图片

4. 创建连接Oracle的java类

 

public class OracleJdbcTest {

	//数据库连接对象

	private static Connection conn = null;

	

	private static String driver = "oracle.jdbc.driver.OracleDriver"; //驱动

	

	private static String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl"; //连接字符串

	

	private static String username = "system"; //用户名

	

	private static String password = "123456"; //密码

	

	

	// 获得连接对象

	private static synchronized Connection getConn(){

		if(conn == null){

			try {

				Class.forName(driver);

				conn = DriverManager.getConnection(url, username, password);

			} catch (ClassNotFoundException e) {

				e.printStackTrace();

			}catch (SQLException e) {

				e.printStackTrace();

			}

		}

		return conn;

	}

	

	//执行查询语句

	public void query(String sql, boolean isSelect) throws SQLException{

		PreparedStatement pstmt;

		

		try {

			pstmt = getConn().prepareStatement(sql);

			//建立一个结果集,用来保存查询出来的结果

			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {

				String name = rs.getString("name");

				System.out.println(name);

			}

			rs.close();

			pstmt.close();

		} catch (SQLException e) {

			e.printStackTrace();

		}

	}

	

	public void query(String sql) throws SQLException{

		PreparedStatement pstmt;

		pstmt = getConn().prepareStatement(sql);

		pstmt.execute();

		pstmt.close();

	}

	

	

	//关闭连接

	public void close(){

		try {

			getConn().close();

			

		} catch (SQLException e) {

			e.printStackTrace();

		}

	}

	

}



	//数据库连接对象

	private static Connection conn = null;

	

	private static String driver = "oracle.jdbc.driver.OracleDriver"; //驱动

	

	private static String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl"; //连接字符串

	

	private static String username = "system"; //用户名

	

	private static String password = "123456"; //密码

	

	

	// 获得连接对象

	private static synchronized Connection getConn(){

		if(conn == null){

			try {

				Class.forName(driver);

				conn = DriverManager.getConnection(url, username, password);

			} catch (ClassNotFoundException e) {

				e.printStackTrace();

			}catch (SQLException e) {

				e.printStackTrace();

			}

		}

		return conn;

	}

	

	//执行查询语句

	public void query(String sql, boolean isSelect) throws SQLException{

		PreparedStatement pstmt;

		

		try {

			pstmt = getConn().prepareStatement(sql);

			//建立一个结果集,用来保存查询出来的结果

			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {

				String name = rs.getString("name");

				System.out.println(name);

			}

			rs.close();

			pstmt.close();

		} catch (SQLException e) {

			e.printStackTrace();

		}

	}

	

	public void query(String sql) throws SQLException{

		PreparedStatement pstmt;

		pstmt = getConn().prepareStatement(sql);

		pstmt.execute();

		pstmt.close();

	}

	

	

	//关闭连接

	public void close(){

		try {

			getConn().close();

			

		} catch (SQLException e) {

			e.printStackTrace();

		}

	}

	

}

 

5. 创建调用类

 

public class ConnOracle {

	public static void main(String[] args) throws SQLException {

		OracleJdbcTest test = new OracleJdbcTest();

		try {

			test.query("drop table student");

		} catch (SQLException e) {}

		

		test.query("create table student(id int, name nchar(20))");

		

		test.query("insert into student values(1,'zhangsan')");

		

		test.query("insert into student values(2,'lisi')");

		

		test.query("select * from student", true);

		

		test.close();

	}

}



	public static void main(String[] args) throws SQLException {

		OracleJdbcTest test = new OracleJdbcTest();

		try {

			test.query("drop table student");

		} catch (SQLException e) {}

		

		test.query("create table student(id int, name nchar(20))");

		

		test.query("insert into student values(1,'zhangsan')");

		

		test.query("insert into student values(2,'lisi')");

		

		test.query("select * from student", true);

		

		test.close();

	}

}

 

6. 显示结果

Java JDBC连接Oracle,java面试题,java初级笔试题_第3张图片

 


转载:https://www.cnblogs.com/linlf03/p/8215677.html

推荐内容:
一次 Java 内存泄漏的排查
初学Java Web(9)——学生管理系统(简易版)总结
Java面试知识点之计算机网络篇(一)
面试 10:玩转 Java 选择和插入排序,附冒泡最终源码
JAVA SSM框架基础面试题
Java 开发岗面试知识点
java面试
Java线程池详解
2年Java开发工作经验面试总结
两年JAVA程序员的面试总结

 

你可能感兴趣的:(java)