java中使用JDBC运用ORACLE完成增删改查的实现

向数据库中插入一条数据
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InsertDemo {
	public static void main(String[] args) {
		String className = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:king";
		String user = "scott";
		String password = "sa";
		String user_name = "scott";
		String user_password = "sa";
		String sql = "insert into userinfo(id,user_name,user_password,create_date) values (sq_userinfo2_id.nextval,'"
				+ user_name + "','" + user_password + "',sysdate)";
		try {
			Class.forName(className);
			Connection connection = DriverManager.getConnection(url, user,
					password);
			Statement statement = connection.createStatement();
			statement.executeUpdate(sql);
			System.out.println("录入成功");
		} catch (Exception e) {
			System.out.println("录入失败");
			e.printStackTrace();
		}
	}
}

从数据库中查询数据

 

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

public class SelectDemo {
	public static void main(String[] args) {
		String className = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:king";
		String user = "scott";
		String password = "sa";
		int id = 3;
		String sql = "select * from userinfo where id=" + id + "";
		try {
			Class.forName(className);
			Connection connection = DriverManager.getConnection(url, user,
					password);
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				System.out.println(resultSet.getString("user_name"));
			}
			System.out.println("查询成功");
		} catch (Exception e) {
			System.out.println("查询失败");
			e.printStackTrace();
		}
	}
}

通过id修改特定的某条数据

 

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

public class UpdateDemo {
	public static void main(String[] args) {
		String className = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:king";
		String user = "scott";
		String password = "sa";
		int id = 3;
		String temp_user_name = "arthur";
		String sql = "update userinfo set user_name= '" + temp_user_name
				+ "' where id=" + id + "";
		try {
			Class.forName(className);
			Connection connection = DriverManager.getConnection(url, user,
					password);
			Statement statement = connection.createStatement();
			statement.executeUpdate(sql);
			System.out.println("更新成功");
		} catch (Exception e) {
			System.out.println("更新失败");
			e.printStackTrace();
		}
	}
}

通过id删除某条数据

 

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

public class DeleteDemo {
	public static void main(String[] args) {
		String className = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:king";
		String user = "scott";
		String password = "sa";
		int id = 3;
		String sql = "delete from userinfo where id=" + id + "";
		try {
			Class.forName(className);
			Connection connection = DriverManager.getConnection(url, user,
					password);
			Statement statement = connection.createStatement();
			statement.executeUpdate(sql);
			System.out.println("删除成功");
		} catch (Exception e) {
			System.out.println("删除失败");
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,oracle,jdbc,插入数据)