使用plsql创建students表
create table students(
id number(4),
name varchar2(20),
sex varchar2(4),
age number(4),
primary key(id)
);
然后创建序列
(
Oracle数据库不会像mysql自动自动生成id
这样有好处也有坏处 id只能自动生成 有时候需要自定义的时候只能只能重新创建一个字段
所以使用Oracle更好一点 序列会自动生成一个顺序 只需要调用nextval方法就行
)
create sequence stu_seq;
然后java操作数据库需要导入 jdbc包
然后新建 model包 dao包 util包
这样就好了
Students类
//类名和表名不一定要相同 属性名 和字段名 必须一致
//javabean :对属性取值和赋值操作的一个组件
public class Students {
int id;
String name;
String sex;
int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DBLink类(用来连接数据库)
//连接orecle数据库
public class DBLink {
public static Connection getCon() throws ClassNotFoundException, SQLException {
//找到oracle驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
"system","zhang");
return connection;
}
}
//StudentDao 对数据进行操作
public class StudentDao {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//插入操作
// Students student= new Students();
// student.setName("张三");
// student.setSex("男");
// student.setAge(22);
// in(student);
//查询操作
// querryAll();
// //登录操作
// Scanner scanner = new Scanner(System.in);
// String name = scanner.next();
// int age = scanner.nextInt();
// login(name,age);
// //修改数据
// updata();
//删除操作
delete();
}
//插入
public static int in(Students student) throws SQLException, ClassNotFoundException {
Connection connection = DBLink.getCon();
//?为占位符 从序号1开始 赋值要在执行前面
String strInsert = "insert into students (id,name,sex,age) " +
"values (stu_seq.nextval,?,?,?)";
//预处理 通过连接 把要执行的sql准备好 马上可以执行
PreparedStatement preparedStatement = connection.prepareStatement(strInsert);
//executeUpdate返回1为执行成功 返回0为失败 execute也可以执行增删改查 返回为boolean
// executeUpdate不能执行查询 但是executeUpdate执行效率更高 返回为int
preparedStatement.setString(1,student.getName());
preparedStatement.setString(2,student.getSex());
preparedStatement.setInt(3,student.getAge());
preparedStatement.executeUpdate();
connection.commit();
return 0;
}
//查询 查询全部 查询对应条件de
public static void querryAll() throws SQLException, ClassNotFoundException {
Connection connection = DBLink.getCon();
String querrySql = "select * from students ";
PreparedStatement preparedStatement = connection.prepareStatement(querrySql);
//查询出来是一个表格 需要放到一个列表中 ResultSet是用来存放查询结果的
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getString("name") + resultSet.getString("age"));
}
}
//登录 查询
public static void login(String name,int age) throws SQLException, ClassNotFoundException {
Connection connection = DBLink.getCon();
//可以使用"+变量+"赋值
String loginSql = "select *from students where name='"+name+"' and age="+age+" ";
PreparedStatement preparedStatement = connection.prepareStatement(loginSql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println("登录成功!");
}
}
//更新
public static void updata() throws SQLException, ClassNotFoundException {
Connection connection = DBLink.getCon();
String updataSql = "UPDATE students set name=? where name='张三' ";
PreparedStatement preparedStatement = connection.prepareStatement(updataSql);
preparedStatement.setString(1,"张三2");
preparedStatement.executeUpdate();
connection.commit();
}
//删除
public static void delete() throws SQLException, ClassNotFoundException {
Connection connection = DBLink.getCon();
String deleteSql = "delete from students where name=?";
PreparedStatement preparedStatement = connection.prepareStatement(deleteSql);
preparedStatement.setString(1,"张三2");
preparedStatement.executeUpdate();
connection.commit();
}
}