欢迎光临散文网 会员登陆 & 注册

MySQL预处理DbTools增properties文件,配置文件,DBUtils分页查询limit【诗书画唱】

2020-08-26 08:06 作者:诗书画唱  | 我要投稿




1、封装一个DbTools工具类,要求使用properties文件实现。




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文件读取到内存中去:

    InputStream Inp = DbTools.class.getClassLoader()

    .getResourceAsStream("./db.properties");

   

//    : 也可以不写“./”

    //加载内容:

    try {

Pro.load(Inp);

//读取内容:

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();

}

    }

    

    //获取数据库连接对象的方法:

    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


不可像下图所示换行不然会报错等:


2、创建一个宠物表,包含id,color,name,weight,通过循环插入15条数据。


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时的代码。

// 有对比的作用,当出现

// “加载不出类”“Access denied for user 

// root\'@\'localhost\'”等错误时,可以先不用

// DbTools实现jdbc的更改,后注释,用DbTools,

// 这样就知道错哪里等。

// 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字符串变成一个真正能够执行的sql语句。

//用prepareStatement(预编译),可以防止注入,

//就是当prepareStatement不用时,

//有人恶意乱输入密码(比如“"”等双引号,让SQL

// 语句处报错。)

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 + "条数据");

}

he++;

}


System.out.println("共成功插入了" +he

+ "条数据");


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭):

try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}




3、在控制台接受输入页码(个人理解:页码为查整张表时的第几行数-1,即为下标)和页面大小(个人理解:页面大小为查整张表时的总行数),根据输入的页码和页面大小将查询到的宠物信息显示在控制台上

比如:

**********************************

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("请输入页码:");

        int page = Sca.nextInt();

        System.out.println("请输入页面大小:");

        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 {

//清理资源(先打开的后关闭):

try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}





当前显示的是第1页的内容,每页显示2条数据(个人的理解:页码就像

是把一段内容分割,第一个内容当作页数比如



ID    颜色    名字    体重




第1页 (下标为0)


1     橘色    咪咪    3


2     白色    鱼鱼    2




3     橘色 2   咪咪 2   3


第5页(下标为4)

4     白色 2   鱼鱼2    2


第6页(下标为5)


5     橘色3    咪咪3    3


6    白色3    鱼鱼 3   2


(每页的内容可以不固定,可以自己定等))


MySQL预处理DbTools增properties文件,配置文件,DBUtils分页查询limit【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律