標籤:
匯入包
<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