java資料訪問(2):DBUtils

來源:互聯網
上載者:User

標籤:

匯入包

        <dependency>            <groupId>commons-dbutils</groupId>            <artifactId>commons-dbutils</artifactId>            <version>1.5</version>        </dependency>        <!-- 匯入Mysql資料庫連結jar包 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.6</version>        </dependency>

 

Bean

package my;public class User {    private Long userid;    private String username;    private String password;    private String comment;    public Long getUserid() {        return userid;    }    public void setUserid(Long userid) {        this.userid = userid;    }     public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }        public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }        public String getComment() {        return comment;    }    public void setComment(String comment) {        this.comment = comment;    }     }

 

添加資料

package my;import java.sql.Connection;import java.sql.SQLException;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;public class Insert {    public static void main(String[] args) throws SQLException {        Connection conn = ConnectionHelper.getConnection();        String sql = "insert into user(username, password) values(?,?)";        QueryRunner queryRunner = new QueryRunner();        try {            Object[] params = { "hehe", "123" };            int i = queryRunner.update(conn, sql, params);            System.out.println(i);        } catch (Exception e) {            e.printStackTrace();        } finally {            DbUtils.close(conn);        }    }}

 

刪除資料

package my;import java.sql.Connection;import java.sql.SQLException;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;public class Delete {    public static void main(String[] args) throws SQLException {        Connection conn = ConnectionHelper.getConnection();        String sql = "delete from user where userId=11";        QueryRunner queryRunner = new QueryRunner();        try {            int i = queryRunner.update(conn, sql);            System.out.println(i);        } catch (Exception e) {            e.printStackTrace();        } finally {            DbUtils.close(conn);        }    }}

 

修改資料

package my;import java.sql.Connection;import java.sql.SQLException;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;public class Update {    public static void main(String[] args) throws SQLException {        Connection conn = ConnectionHelper.getConnection();        String sql = "update user set userName=?, password=?  where userId=?";        QueryRunner queryRunner = new QueryRunner();        try {            Object[] params = { "wowo", "123", 10 };            int i = queryRunner.update(conn, sql, params);            System.out.println(i);        } catch (Exception e) {            e.printStackTrace();        } finally {            DbUtils.close(conn);        }    }}

 

擷取一條資料

package my;import java.sql.Connection;import java.sql.SQLException;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.ResultSetHandler;import org.apache.commons.dbutils.handlers.BeanHandler;public class Select1 {    public static void main(String[] args) throws SQLException {        Connection conn = ConnectionHelper.getConnection();        String sql = "select * from user";        QueryRunner queryRunner = new QueryRunner();        try {            ResultSetHandler<User> handler = new BeanHandler<User>(User.class);            User user = queryRunner.query(conn, sql, handler);            System.out.println(user.getUserid() + "  " + user.getUsername()                    + "  " + user.getPassword());        } catch (Exception ex) {            ex.printStackTrace(System.out);        } finally {            DbUtils.close(conn);        }    }}

 

擷取多條資料

package my;import java.sql.Connection;import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.ResultSetHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;public class SelectList {    public static void main(String[] args) throws SQLException {        Connection conn = ConnectionHelper.getConnection();        String sql = "select * from user";        QueryRunner queryRunner = new QueryRunner();        try {            ResultSetHandler<List<User>> handler = new BeanListHandler<User>(                    User.class);            List<User> users = queryRunner.query(conn, sql, handler);            for (User user : users) {                System.out.println(user.getUserid() + "  " + user.getUsername()                        + "  " + user.getPassword());            }        } catch (Exception ex) {            ex.printStackTrace(System.out);        } finally {            DbUtils.close(conn);        }    }}

 

java資料訪問(2):DBUtils

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.