JDBC连接数据库更新操作

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/--

     - 

     - 执行更新

     - @param string

     - @param sql

     -/

    public void execute(String sql){

        try {

            stmt = getConn().createStatement();

            stmt.executeUpdate(sql); //执行SQL语句增删改的方法

            //this.conn.commit();

        } catch (SQLException e1) {

            // TODO Auto-generated catch block

            e1.printStackTrace();

        }



    }

/--

     - 查询方法

     - @return

     -/

    public int selMat(int i,String sql){

        try {

            stmt = this.getConn().createStatement();

            rs   = stmt.executeQuery(sql); //执行查询SQL语句的方法

        if(i == 1){//全部查询

            while(rs.next()){

                System.out.println(rs.getString("s_id")+rs.getString("s_name"));

            }

        } else{//汇总查询

            rs.next();

            int j = rs.getInt(1);

            System.out.println(j);

        }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return 0;

    }

 

你可能感兴趣的:(jdbc)