JDBC-数据库连接技术1

运用JDBC技术:前提要导入连接池包:(在项目名下导入)


完整实例
package day01;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 查询emp表的信息
 */
public class TestEmp {
	public static void main(String[] args) throws Exception {
		//1:加载驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//2:获取连接对象
		Connection conn = 
				DriverManager
				.getConnection(
				"jdbc:oracle:thin:@127.0.0.1:1521:orcl", 
				"scott", 
				"1234");
		//3:创建sql语句编译器
		Statement stat = conn.createStatement();
		String sql = "select * from emp";
		//4:处理结果集
		ResultSet rs = stat.executeQuery(sql);
		while(rs.next()){
			//从当前行中取数据
			/*
			 *  rs.getType(int index)
			 *  index:从1开始,表示第index个字段名   
			 *  rs.getType(String colName)
			 *  指定字段名称 
			 *  返回值是字段对应的值。
			 */
		  int empno = rs.getInt(1);
		  String ename = rs.getString("ename");
		  String job = rs.getString("job");
		  int mgr = rs.getInt("mgr");
		  Date hiredate = rs.getDate("hiredate");
		  double sal = rs.getDouble("sal");
		  double comm = rs.getDouble("comm");
		  int deptno = rs.getInt(8);
		  System.out.println(empno+","+ename+","+deptno);
		}
		//5:关闭连接
		conn.close();
		
	}
}

连接数据库的方法1:-DBUtil
package util;
//版本1
import java.sql.Connection;
import java.sql.DriverManager;

/** 连接数据库的工具类 */
public class DBUtil1 {
	public static Connection getConn() {
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");//1加载驱动
			conn = DriverManager.getConnection(//2.建立连接
					"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "1234");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeConn(Connection conn) {
		try {
			if(conn!=null){    //5.关闭连接
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
        public static void main(String[] args) {
		System.out.println(getConn());
	}
}
连接数据库方法2--配置文件properties配置连接参数
package util;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * 版本2:
 * 将字符串信息,提取出去,从配置文件中获取
 */
public class DBUtil2 {
	private static String driver;
	private static String url;
	private static String user;
	private static String pwd;
	/**读取配置文件,只需要加载一次*/
	static{
		try {
			FileReader fr = new FileReader("db.properties");
			//创建配置文件对象
			Properties prop = new Properties();
			prop.load(fr);
			driver = prop.getProperty("driver");
			url = prop.getProperty("url");
			user = prop.getProperty("user");
			pwd = prop.getProperty("pwd");
			Class.forName(driver);//1加载驱动
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConn() {
		Connection conn = null;
		try {			
			conn = DriverManager.getConnection( //2.建立连接
					url, user, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeConn(Connection conn) {
		try {
			if(conn!=null){  //3.关闭连接
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
//  配置文件db.properties,放在工程名下,连接的是Oracle
driver=oracle.jdbc.driver.OracleDriver
#driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
pwd=1234 



猜你喜欢

转载自blog.csdn.net/xiaozelulu/article/details/80253155