当前位置:   article > 正文

java 连接oralce数据库,并对其进行增删改查操作(连接本地数据库报密码已过期)_jdbc 发生了连接权限故障。原因:密码已到期

jdbc 发生了连接权限故障。原因:密码已到期

今天准备连接oracle本地数据库,发现一个问题,就是连接本地数据库报,密码已过期,上网搜了一下,是由于 由于oracle11g中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”,导致密码过期,程序无法使用

解决方法:window+R 输入cmd打开窗口输入

sqlplus 然后数据用户名和以前的密码会报 the password has expired
这个错,然后会提示你重新输入新口令(密码),然后重新登录即可。

连接oralce数据库前需要先添加驱动

一般装oralce中会自带驱动文件
E:\oracle\product\11.2.0\dbhome_1\jdbc\lib(这是我的驱动文件路径,我装的是在E盘)如果找不到就到网上重新下载一个驱动
1 新建一个项目,创建lib文件夹
2.把刚才找到的驱动放到这个文件里
3.点项目右键选择Build Path 点Configure Build Path选择Libraries 然后Add JARs 把lib文件下的驱动放进去.

了解一下连接数据库需要用到的东西

一 DriverManager类
DriverManage类用来管理数据库中的所有驱动程序,是JDBC的管理层,作用于用户和驱动程序之间,跟踪可用的驱动程序,并在数据库的驱动之间建立连接,DriverManager类中的方法都是静态方法,下面是常用方法:
getConnection(String URL,String user,String PassWord) 指定3个参数,分别是连接地址,用户名 和密码 类连接数据库
setLoginTimeout() 获取驱动程序视图登陆到某一数据库可以等待的最长时间,以秒为单位,
Println(String message) 将一条信息打印到当前JDBC日志流中

二 Connection 接口
COnnection 接口代表与特定的数据库的连接,要对数据库中的数据进行操作,首先要获取数据库连接,Connection实就像在应用程序与数据库之间开通了一条通道.可以通过DriverManager类的getConnection()方法获取Connection的实例.Connection接口的常用方法如下:
createStatement() 创建Statement对象
PrepareStatement() 创建预处理对象PrepareStatement
isReadOnly() 查看当前Connection对象的读取模式是否是只读形式
SetReadOnly() 设置当前Connection对象的读写模式,默认是非只读模式
close() 立即释放此Connection对象的数据库和JDBC资源,而不是自动释放

三 Statement 接口
Statement接口用于创建向数据库中传递SQL语句的对象, 常用方法如下:
execute(String sql) 执行静态的SELECT语句 可能返回多个数据集
executeQuery(String sql) 执行给定的Sql语句 返回单个ResultSet对象
clearBatch() 清空此Statement对象的当前SQL命令列表
executeUpdate() 执行指定的SQL语句 该语句可以为INSERT UPDATE DELETE语句
close() 释放Statement实例占用的数据库和JDBC资源

四 PreparedStatement 接口
PreparedStatement接口继承Statement,用于执行动态的SQL语句 ,通过PreparedStatement实例执行的SQL语句,将被编译并保存到PreparedStatement实例中,从而可以重复的执行该SQL语句 . preparedStatement接口的常用方法:
execute( ) 在此PreparedStatement对象执行SQL语句,该语句可以是任何类型的SQL语句
executeQuery( ) 在此preparedStatement对象中执行Sql查询语句,返回为查询数据集的对象
executeUpdate() 在此preparedStatement对象执行Sql语句,该SQL语句必须是一个INSERT UPDATE DELETE语句,或者是没有返回值得DLL语句
setbyte(int Pindex byte by) 将参数Pindex位置上设置为给定的byte参数by
setString(int Pindex String str)将参数Pindex位置上设置为给定的String参数值str
setDouble(int pindex Double dou)将参数Pindex位置上设置为给定的Double参数值dou
setInt(int Pindex int i) 将参数Pindex位置上设置为给定的int参数值i
setObject(int PIndex Ocject obj)将参数Pindex位置上设置为给定的Object参数值Obj

五 ResultSet 接口
ResultSet接口类似于一个临时的数据表,用来暂时存放数据库查询操作获取到的数据集 它的常用方法如下:
getint() 以int形式获取当前行指定的列
getFloat() 以Float形式获取当前行指定的列
getDate() 以Date形式获取当前行指定的列
getBoolean() 以Boolean形式获取当前行指定的列
getString() 以String形式获取当前行指定的列
getObject() 以Object形式获取当前行指定的列
next( ) 将指针向下移一行
updateInt() 用int值更新指定列
updateFloat()用float值更新指定列
updateLong() 用指定的long值更新指定的列
updateString()用指定的"String"值更新指定列
updateObejct() 用object值更新指定的列
updatenull() 将指定的列值修改为null
updateDate() 用指定的Date值更新指定的列
updateDouble() 用指定的DOuble值更新指定的列

-----------下面开始连接Oracle数据库-----------------

连接数据库

public class DBConn {
	static Connection con;// 声明Connection对象
	static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	static String User = "scott";
	static String Password = "scott";
	static String Driver = "oracle.jdbc.driver.OracleDriver";

	// 连接数据库
	public static Connection getConnection()  { // 建立返回值为Connection的方法
		// 加载数据库驱动类
		try {
			Class.forName(Driver);
			System.out.println("数据库驱动加载成功"); // 返回加载驱动成功信息
			con = DriverManager.getConnection(url, User, Password);// 通过访问数据库的URL获取数据库连接对象。这里后两个参数分别是数据库的用户名及密码
			System.out.println("数据库连接成功"); // 返回连接成功信息
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;// 按方法要求返回一个Connection对象
	}
	public static void main(String[] args) throws Exception { 
		getConnection();// 调用数据库的方法
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

查询

/*
 * 查询
 */
public static void select() {
	Connection con;
	PreparedStatement ps;// 预编译对象
	ResultSet rs = null;// 结果集
	// 连接对象
	con = DBConn.getConnection();
	if (con == null) {
		return;
	}
	try {
		String sql = "select * from login_cs";// sql语句
		ps = con.prepareStatement(sql);//
		rs = ps.executeQuery();// 查询
		while (rs.next()) {
			System.out.println("用户名   " + rs.getString("user_name") + "\r\n" + "密码  " + rs.getString("password"));
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		// 释放资源
		try {
			rs.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

增加(第一种方式)

/*
	 * 增加
	 */
	public static void add() {
		Connection con;
		PreparedStatement ps;// 预编译对象
		// ResultSet rs = null;// 结果集
		// 连接对象
		con = DBConn.getConnection();
		if (con == null) {
			return;
		}
		// 获取用户输入的用户名和密码
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String user_name = scanner.nextLine();
		System.out.println("请输入密码:");
		String password = scanner.nextLine();
		// 定义一个sql
		try {
			String sql = "insert into login_cs values ('" + user_name +"','" + password + "')";
			ps = con.prepareStatement(sql);
			int i = ps.executeUpdate();
			if (i > 0) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

增加(第二种方式)

	/*
	 * 增加
	 */
	public static void add2() {
		Connection con;
		PreparedStatement ps;// 预编译对象
		// ResultSet rs = null;// 结果集
		// 连接对象
		con = DBConn.getConnection();
		if (con == null) {
			return;
		}
		// 获取用户输入的用户名和密码
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String user_name = scanner.nextLine();
		System.out.println("请输入密码:");
		String password = scanner.nextLine();
		// 定义一个sql
		try {
			String sql = "insert into login_cs values (?,?)";
			ps = con.prepareStatement(sql);
			ps.setString(1, user_name);
			ps.setString(2, password);
			int i =ps.executeUpdate();
			if (i > 0) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

删除

/*
	 * 删除
	 */
	public static void delete() {
		Connection con;
		PreparedStatement ps;// 预编译对象
		// ResultSet rs = null;// 结果集
		// 连接对象
		con = DBConn.getConnection();
		if (con == null) {
			return;
		}
		// 获取用户输入的用户名和密码
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入要删除的用户:");
		String user_name = scanner.nextLine();
		// 定义一个sql
		try {
			String sql = "delete from login_cs where user_name= '"+user_name+"'";
			ps = con.prepareStatement(sql);
			int i = ps.executeUpdate();
			if (i > 0) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

更新

/*
	 * 更新
	 */
	public static void update() {
		Connection con;
		PreparedStatement ps;// 预编译对象
		// ResultSet rs = null;// 结果集
		// 连接对象
		con = DBConn.getConnection();
		if (con == null) {
			return;
		}
		// 获取用户输入的用户名和密码
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入要修改的用户:");
		String user_name = scanner.nextLine();
		System.out.println("请输入新的密码");
		String password = scanner.nextLine();
		// 定义一个sql
		try {
			String sql = "update login_cs set password= '"+ password+"' where user_name= '"+user_name+"'";
			ps = con.prepareStatement(sql);
			int i = ps.executeUpdate();
			if (i > 0) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//释放资源
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号