JDBC CRUD

private static Connection getConn() {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        String username = "excel";
        String password = "excel";
        Connection conn = null;
        try {
            Class.forName(driver);
            // new oracle.jdbc.driver.OracleDriver();
            conn = DriverManager.getConnection(url, username, password);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    private static int insert(Type type) {
        Connection conn = getConn();
        int i = 0;
        String sql = "insert into PARAM_DELTAIL (id,param_type,param_name,param_value,locale,seq,remark) values(?,?,?,?,?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);
            // Statement stat = conn.createStatement();
            pstmt.setString(1, type.getId());
            pstmt.setString(2, type.getParamType());
            pstmt.setString(3, type.getParamName());
            pstmt.setString(4, type.getParamValue());
            pstmt.setString(5, type.getLocale());
            pstmt.setInt(6, type.getSeq());
            pstmt.setString(7, type.getRemark());
            i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    private static List query() {
        Connection conn = getConn();
        String sql = "select t.type_name,t.type,t.remark from class1 t order by t.type_id asc,t.type_name asc";
        PreparedStatement pstmt;
        int count = 0;
        List typeList = new ArrayList();
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
            	Type type = new Type(rs.getString("type").trim(),rs.getString("type_name").trim());
            	type.setRemark(rs.getString("remark"));
            	typeList.add(type);
                count++;
            }
            System.out.println("共查询到记录(条)"+ count);
            rs.close();
            pstmt.close();
            conn.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
		return typeList;

    }
    
    private static Integer getSeq(){
    	Connection conn = getConn();
        String sql = "select SEQ_DETIAL.Nextval from dual";
        PreparedStatement pstmt;
        try {
			pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			return rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
    
    }

    private static int update(Type type) {
        Connection conn = getConn();
        int i = 0;
        String sql = "update users set password='" + type.getParamType()
                + "' where username='" + type.getParamName() + "'";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);

            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);

            pstmt.close();
            conn.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }
    
    private static int delete(String username) {
        Connection conn = getConn();
        int i = 0;
        String sql = "delete users where username='" + username + "'";
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement(sql);

            i = pstmt.executeUpdate();
            System.out.println("resutl: " + i);

            pstmt.close();
            conn.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }


 

你可能感兴趣的:(JDBC,jdbc,string,sql,insert,delete,excel)