短学期小结2
mybatis的建立
Dbutils.java:
package cn.neusoft.mybatis.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dbutils {
?? ?
?? ?private static final String DRIVER = "com.mysql.jdbc.Driver";
?? ?private static final String URL = "jdbc:mysql://localhost:3306/mybatis";
?? ?private static final String UNAME = "root";
?? ?private static final String UPWD = "123";
?? ?
?? ?private static Connection con = null;//連接對象 ?
?? ?private static ResultSet rs = null;//放查詢結果的
?? ?private static PreparedStatement ps = null;//放sql文并且可以拼接sql文的對象
?? ?
?? ?static{
?? ??? ?try {//驅動加載
?? ??? ??? ?Class.forName(DRIVER);
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?
?? ?//獲取連接
?? ?public static void getConnection(){
?? ??? ?try {
?? ??? ??? ?con = DriverManager.getConnection(URL, UNAME, UPWD);
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?//關閉連接
?? ?public static void closeAll(){
?? ??? ?if(null!=rs){
?? ??? ??? ?try {
?? ??? ??? ??? ?rs.close();
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(null!=ps){
?? ??? ??? ?try {
?? ??? ??? ??? ?ps.close();
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(null!=con){
?? ??? ??? ?try {
?? ??? ??? ??? ?con.close();
?? ??? ??? ?} catch (SQLException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?//執行查詢
?? ?public static ResultSet executeQuery(String sql,Object[] obj){
?? ??? ?getConnection();
?? ??? ?//System.out.println(obj);
?? ??? ?//如果sql文不完整,是需要進行拼接的
?? ??? ?try {
?? ??? ??? ?ps = con.prepareStatement(sql);
?? ??? ??? ?
?? ??? ??? ?if(null!=obj){
?? ??? ??? ??? ?for(int i=0;i<obj.length;i++){
?? ??? ??? ??? ??? ?ps.setObject(i+1, obj[i]);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?rs = ps.executeQuery();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return rs;
?? ?}
?? ?//公共的執行? 增、刪、改 操作 insert? delete?? update
?? ?public static int executeUpdate(String sql,Object[] obj){
?? ??? ?int count = 0;
?? ??? ?getConnection();
?? ??? ?try {
?? ??? ??? ?ps = con.prepareStatement(sql);
?? ??? ??? ?if(null!=obj){
?? ??? ??? ??? ?for(int i=0;i<obj.length;i++){
?? ??? ??? ??? ??? ?ps.setObject(i+1, obj[i]);
?? ??? ??? ??? }
?? ??? ??? ?}
?? ??? ??? ?count = ps.executeUpdate();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?closeAll();
?? ??? ?}
?? ??? ?return count;
?? ?}
?? ?//如果要使用創表語句之類,可以使用如下代碼
?? ?public static boolean executeCreate(String sql,Object[] obj){
?? ??? ?getConnection();
?? ??? ?boolean b =false;
?? ??? ?try {
?? ??? ??? ?ps = con.prepareStatement(sql);
?? ??? ??? ?if(null!=obj){
?? ??? ??? ??? ?for(int i=0;i<obj.length;i++){
?? ??? ??? ??? ??? ?ps.setObject(i+1, obj[i]);
?? ??? ??? ??? ?}
?? ??? ??? ?b = ps.execute();
?? ??? ??? ?}
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?closeAll();
?? ??? ?}
?? ??? ?if(b){
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
}
JDBC測試包:
package cn.neusoft.mybatis.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJdbc {
?? ?/**
?? ? *jdbc? 主要用于? java程序? 訪問數據庫
?? ? */
?? ?public static void main(String[] args) {
?? ??? ?//連接信息
?? ??? ?//數據庫鏈接
?? ??? ?Connection con = null;
?? ??? ?//預編譯對象
?? ??? ?PreparedStatement ps = null;
?? ??? ?//如果要進行查詢操作,需要使用??? 結果集對象
?? ??? ?ResultSet rs = null;?? ??? ?
?? ??? ?try {
?? ??? ??? ?//加載驅動
?? ??? ??? ?Class.forName("com.mysql.jdbc.Driver");
?? ??? ??? ?//獲取連接
?? ??? ??? ?//jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
?? ??? ??? ?//?? ??? ??? ??? ?localhost代表訪問地址?? 可以是127.0.0.1 也可以是localhost
?? ??? ??? ?//????????????? 還可以是ip地址?? 3306代表端口號???? mybatis是數據庫名?? 嚴格區分大小寫
?? ??? ??? ?con = DriverManager.getConnection(
?? ??? ??? ??? ??? ?"jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf-8",
?? ??? ??? ??? ??? ?"root",
?? ??? ??? ??? ??? ?"123");
?? ??? ??? ?//sql 文處理???? ? 只是一個占位符
?? ??? ??? ?String sql = "select * from userinfo where username = ?";
?? ??? ??? ?// *? 代替所有? 對于本句 包含了? userid,username,pwd? 三列
?? ??? ??? ?//實際開發 寫成?? select userid,username,pwd from userinfo where
?? ??? ??? ?//??????????????? username=?
?? ??? ??? ?//把sql 文放入預編譯對象進行處理
?? ??? ??? ?ps = con.prepareStatement(sql);
?? ??? ??? ?//給ps? 對象? 傳入你要具體查詢的人的信息?? 相當于? 給之前sql 文 的? ?? 傳值
?? ??? ??? ?ps.setString(1, "張三");
?? ??? ??? ?//這里的方法是執行一個查詢操作,需要把查詢結果放在ResultSet中
?? ??? ??? ?//?? 如果是要執行? 插入、刪除、更新操作??? ps.execute();
?? ??? ??? ?rs = ps.executeQuery();
?? ??? ??? ?//把查詢結果? 從rs 對象中取出來
?? ??? ??? ?while(rs.next()){
?? ??? ??? ??? ?//? rs.getInt("userid")? 是sql文中的? userid?? ??? ?
?? ??? ??? ??? ?int userid = rs.getInt("userid");
?? ??? ??? ??? ?String username = rs.getString("username");
?? ??? ??? ??? ?String pwd = rs.getString("pwd");
?? ??? ??? ??? ?
?? ??? ??? ??? ?System.out.println("編號:"+userid+"姓名:"+username+"密碼"+
?? ??? ??? ??? ?pwd);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
}
示例:
有1000條數據??
每頁只會顯示部分? ,每頁顯示20條數據
第一頁? 顯示1-20條
第二頁? 顯示21-40條
…
?問:每頁顯示3條記錄的情況
(每頁顯示幾條? 以及? 頁碼)
(3n-2,3n)
select * from userinfo limit 3(n-1),3;
select * from userinfo limit? pageSize(pageNo-1),pageSize;
?
package cn.neusoft.mybatis.pojo;
?
實體類Userinfo的建立://主要
/**
?*?? Userinfo? 是一個實體類? 用于 與表作對應的實體映射文件
?*?? ??? ?該類中?? 各個屬性?? 對應?? userinfo 表中的屬性
?*/
public class Userinfo {
?? ?private Integer userid;
?? ?private String username;
?? ?private String pwd;
?? ?
?? ?@Override
?? ?public String toString() {
?? ??? ?return "Userinfo [userid=" + userid + ", username=" + username
?? ??? ??? ??? ?+ ", pwd=" + pwd + "]";
?? ?}
?? ?//生成 get set 方法??????? alt+shift+s? 選擇 set get 方法生成
?? ?//生成構造方法 ?
?? ?//無參的構造方???? 如果不寫的話,默認生成一個?? 無參的構造方法
?? ?public Userinfo() {
?? ??? ?super();
?? ?}
?? ?//有參的構造方法
?? ??? ?public Userinfo(String username, String pwd) {
?? ??? ?super();
?? ??? ?this.username = username;
?? ??? ?this.pwd = pwd;
?? ?}
?? ?public Integer getUserid() {
?? ??? ?return userid;
?? ?}
?? ?public void setUserid(Integer userid) {
?? ??? ?this.userid = userid;
?? ?}
?? ?public String getUsername() {
?? ??? ?return username;
?? ?}
?? ?public void setUsername(String username) {
?? ??? ?this.username = username;
?? ?}
?? ?public String getPwd() {
?? ??? ?return pwd;
?? ?}
?? ?public void setPwd(String pwd) {
?? ??? ?this.pwd = pwd;
?? ?}
?? ?
?? ?
}
轉載于:https://www.cnblogs.com/arfang/p/7100462.html
總結
- 上一篇: 快速了解一门技术的基本步骤
- 下一篇: HttpHelper之我见