MySQL預(yù)處理DbTools增properties文件,配置文件,DBUtils分頁查詢limit【詩書畫唱】

1、封裝一個(gè)DbTools工具類,要求使用properties文件實(shí)現(xiàn)。


package jdbcMySQL;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;
public class DbTools {
? ? private static String driverName;
? ? private static String url;
? ? private static String user;
? ? private static String pwd;
? ??
? ? static {
? ? //讀取properties文件:
? ? Properties Pro = new Properties();
? ? //將db.properties文件讀取到內(nèi)存中去:
? ? InputStream Inp = DbTools.class.getClassLoader()
? ? .getResourceAsStream("./db.properties");
? ?
//? ? : 也可以不寫“./”
? ? //加載內(nèi)容:
? ? try {
Pro.load(Inp);
//讀取內(nèi)容:
driverName = Pro.getProperty("driverNameProperty");
//System.out.println(driverName);
url = Pro.getProperty("urlProperty");
user = Pro.getProperty("userProperty");
pwd = Pro.getProperty("pwdProperty");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? }
? ??
? ? //獲取數(shù)據(jù)庫連接對(duì)象的方法:
? ? public static com.mysql.jdbc.Connection getConn(){
? ? Connection Con = null;
? ? try {
Class.forName(driverName);
Con = DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}? ?
? ? return (com.mysql.jdbc.Connection) Con;
? ? }
? ??
? ? public static void close(ResultSet Res,PreparedStatement Pre
? ? ,Connection Con){
? ? ? ? try {
? ? ? ? if(Res != null) {
? ? ? ? ? ? Res.close();
? ? ? ? ? ? }
? ? ? ? ? ? if(Pre != null) {
? ? ? ? ? ? Pre.close();
? ? ? ? ? ? }
? ? ? ? ? ? if(Con != null) {
? ? ? ? ? ? Con.close();
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ??
}


driverNameProperty=com.mysql.jdbc.Driver
urlProperty=jdbc:mysql://localhost:3306/j190802?useUnicode=true&characterEncoding=UTF-8
userProperty=root
pwdProperty=root

不可像下圖所示換行不然會(huì)報(bào)錯(cuò)等:


2、創(chuàng)建一個(gè)寵物表,包含id,color,name,weight,通過循環(huán)插入15條數(shù)據(jù)。
create table cw(
id int primary key auto_increment,
color varchar(100) ,
name varchar(100) ,
weight float
)


package jdbcMySQL;
import java.sql.*;
import java.util.Date;
public class insertCWDBUtils {
public static void main(String[] args) {
//下面的注釋的代碼都是不用DBUtils,即為DbTools時(shí)的代碼。
// 有對(duì)比的作用,當(dāng)出現(xiàn)
// “加載不出類”“Access denied for user?
// root\'@\'localhost\'”等錯(cuò)誤時(shí),可以先不用
// DbTools實(shí)現(xiàn)jdbc的更改,后注釋,用DbTools,
// 這樣就知道錯(cuò)哪里等。
// String driverName = "com.mysql.jdbc.Driver";
//
// String url = "jdbc:mysql://127.0.0.1:3306"
// + "/j190802?useUnicode=true&"
// + "characterEncoding=UTF-8";
// String user = "root";
// String pwd = "root";
Connection Con = null;
PreparedStatement Pre = null;
ResultSet Res = null;
try {
// Class.forName(driverName);
Con = DbTools.getConn();
System.out.println(Con);
String sql = "insert into cw "
?+ "(color,name ,weight )values(?,?,?)";
//將sql字符串變成一個(gè)真正能夠執(zhí)行的sql語句。
//用prepareStatement(預(yù)編譯),可以防止注入,
//就是當(dāng)prepareStatement不用時(shí),
//有人惡意亂輸入密碼(比如“"”等雙引號(hào),讓SQL
// 語句處報(bào)錯(cuò)。)
Pre = Con.prepareStatement(sql);
int he=0;
String? name;
for(int i=1;i<=15;i++){
Pre.setString(1,"白色");
Pre.setString(2, "詩書畫唱的狗狗"+i);
Pre.setFloat(3,666f+i);
int count = Pre.executeUpdate();
if(count == 0) {
System.out.println("插入失敗");
} else {
System.out.println("成功插入了" + count + "條數(shù)據(jù)");
}
he++;
}
System.out.println("共成功插入了" +he
+ "條數(shù)據(jù)");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源(先打開的后關(guān)閉):
try {
if(Pre != null) {
Pre.close();
}
if(Con != null) {
Con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}




3、在控制臺(tái)接受輸入頁碼(個(gè)人理解:頁碼為查整張表時(shí)的第幾行數(shù)-1,即為下標(biāo))和頁面大?。?strong>個(gè)人理解:頁面大小為查整張表時(shí)的總行數(shù)),根據(jù)輸入的頁碼和頁面大小將查詢到的寵物信息顯示在控制臺(tái)上
比如:
**********************************
ID? ? 顏色? ? 名字? ? 體重
1? ? ?橘色? ? 咪咪? ? 3
2? ? ?白色? ? 魚魚? ? 2
***********************************

package jdbcMySQL;
import java.sql.*;
import java.util.Date;
import java.util.Scanner;
public class maye {
public static void main(String[] args) {
Connection Con = null;
PreparedStatement Pre = null;
ResultSet Res = null;
try {
// Class.forName(driverName);
Con = DbTools.getConn();
//下面是用上limit的分頁查詢:
? ? ? ? Scanner Sca = new Scanner(System.in);
? ? ? ? String sql = "select * from cw limit ?,?";
? ? ? ? System.out.println("請(qǐng)輸入頁碼:");
? ? ? ? int page = Sca.nextInt();
? ? ? ? System.out.println("請(qǐng)輸入頁面大?。?#34;);
? ? ? ? int row = Sca.nextInt();
? ? ? ? int hideCount = (page - 1) * row;
? ? ? ? Pre = Con.prepareStatement(sql);
? ? ? ? Pre.setInt(1, hideCount);
? ? ? ? Pre.setInt(2, row);
? ? ? ? Res = Pre.executeQuery();
? ? ? ? ? while(Res.next()) {
? ? ? ? ? ? ? System.out.println(Res.getObject(1)+
? ? ? "\t"+Res.getObject(2)+"\t"
? ? ? +Res.getObject(3)+"\t"
? ? ? +Res.getObject(4));}
? ? ? ? ? ? ? ? ??
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源(先打開的后關(guān)閉):
try {
if(Pre != null) {
Pre.close();
}
if(Con != null) {
Con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}


當(dāng)前顯示的是第1頁的內(nèi)容,每頁顯示2條數(shù)據(jù)(個(gè)人的理解:頁碼就像
是把一段內(nèi)容分割,第一個(gè)內(nèi)容當(dāng)作頁數(shù)比如
:
ID? ? 顏色? ? 名字? ? 體重
第1頁 (下標(biāo)為0)
1? ? ?橘色? ? 咪咪? ? 3
2? ? ?白色? ? 魚魚? ? 2
3? ? ?橘色 2? ?咪咪 2? ?3
第5頁(下標(biāo)為4)
4? ? ?白色 2? ?魚魚2? ? 2
第6頁(下標(biāo)為5)
5? ? ?橘色3? ? 咪咪3? ? 3
6? ? 白色3? ? 魚魚 3? ?2
(每頁的內(nèi)容可以不固定,可以自己定等))
