设为首页 加入收藏

TOP

oracle学习笔记(六) JDBC使用(二)
2019-09-17 18:38:39 】 浏览:73
Tags:oracle 学习 笔记 JDBC 使用
} if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 读取属性文件中的信息 * * @param key * @return */ private static String getValue(String key) { // 资源包绑定 ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); return bundle.getString(key); } public static void main(String[] args) { System.out.println(getValue("jdbc.driver")); System.out.println(getConnection()); } }

StudentDao.java

我的代码还没有补全,使用的Statement语句

package homework;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author StarsOne
 * @date Create in  2019/4/24 0024 22:24
 * @description
 */
class StudentDao {


    /**
     * 添加一个
     * @param student
     */
    public void save(Student student) {
        String name = student.getName();
        int num = student.getNum();
        Connection connection = null;
        Statement statement = null;
        try {
            connection = JdbcUtil.getConnection();

            statement = connection.createStatement();

            //拼接SQL语句,把数据插入到数据库中
            statement.execute("insert into STUDENT value(" + num + "," + name + ") ");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, statement, null);
        }
    }

    public void update(Student student) {

    }

    /**
     * 删除指定编号的学生
     * @param num
     */
    public void delete(Integer num) {

        Connection connection = null;
        Statement statement = null;
        try {
            connection = JdbcUtil.getConnection();

            statement = connection.createStatement();

            //拼接SQL语句,把数据插入到数据库中
            boolean flag = statement.execute("delete from  STUDENT where num = " + num);
            if (flag) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, statement, null);
        }
    }
    /**
     * 主键查询
     * @param number
     */
    public void findByPK(Integer number) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet=null;
        try {
            connection = JdbcUtil.getConnection();

            statement = connection.createStatement();

            //查询query,返回一个数据集
             resultSet = statement.executeQuery("select * from STUDENT where num =" + number);
            while (resultSet.next()) {
                int num = resultSet.getInt("num");
                String name = resultSet.getString("name");
                String sex = resultSet.getString("sex");
                int age = resultSet.getInt("age");
                Date birthdate = resultSet.getDate("birthdate");
                double grade = resultSet.getDouble("grade");
                //新建一个对象
                new Student(num, age, grade, name, sex, birthdate);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(connection, statement, resultSet);
        }
    }

    /**
     * 查询全部
     * @return
     */
    public List<Student> findAll() {
        List<Student> list = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet=null;
        try {
            connection = JdbcUtil.getConnection();

            statement = connection.createStatement();

            //查询query,返回一个数据集
            resultSet = statement.executeQuery("select * from STUDENT" );
            while (resultSet.next()) {
                int num = resultSet.getInt("num");
                String name = resultSe
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ORACLE根据两个表都含有的字段条.. 下一篇MySQL 字符集utf8和utf-8的关系

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目