Android JDBC简单使用

一,JDBC 简介

JDBC:java database connectivity java数据库连接

常用的数据库mysql oracle sqlserver

sun公司提供了一套jdbc的接口,让数据库厂商实现。

二,JDBC使用:

1,JDBC简单使用

    步骤:

       1),导入jar包   

public class JDBCTest {

   @Test

   public void jdbcTest() {

       try {

          // [1]注册mysql驱动

           //DriverManager.registerDriver(new Driver());    

           Class.forName("com.mysql.jdbc.Driver");

           

           // [2]连接数据库

           // jdbc:subprotocol:subname

           //  URL 固定格式: jdbc:数据库名字// localhost:3306/ lol (本地数据库地址),用户名,密码

           Connection connection = DriverManager.getConnection(

                   "jdbc:mysql://localhost:3306/lol", "root", "1q2w3e");

          // [3]创建一个statement对象 类似 库管

           Statement statement = connection.createStatement();

           // [4]执行查询语句

           ResultSet resultSet = statement.executeQuery("select * from student");

           resultSet.next();

           String name = resultSet.getString(2);

           System.out.println("name = "+name);

           

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

   }

}

2,JDBC类重要的方法:

    【1】DriverManager:注册驱动 告诉当前程序 连接哪个数据库       

            DriverManager.registerDriver(new Driver());

            Class.forName("com.mysql.jdbc.Driver");

            获取连接 :“ DriverManager.getConnection("jdbc:mysql:///mydb1", "root", "1q2w3e");

    【2】Connection: 连接数据库的对象

            获取一个statement                connection.createStatement()

            获取一个preparedStatement

    【3】Statement: 用来执行sql语句 是一个库管对象

            接口 Statement:用于执行静态 SQL 语句并返回它所生成结果的对象。

            方法:

                executeQuery(String sql)    执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。

    【4】ResultSet: 结果集对象 里面有一个光标 默认在第一行之前 调用next方法 就可以向下移动

            接口 ResultSet: 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。

            方法:getString(String columnLabel)   以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。

                      executeUpdate(String sql) 执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。

    为什么使用     Class.forName("com.mysql.jdbc.Driver");创建对象:

    // 本身方法也是new Driver。我们用Class去调用免得重复创建对象 Driver 类里自己静态代码块进行加载了自己

3,JDBC简单调用增删改查:

public class JDBCSample {

    @Test

    public void add() {

        //插入语句

        try {

            //注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            //链接数据库

            Connection connection = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/lol", "root", "1q2w3e");

            //申请对象类似于库管

            Statement statement = connection.createStatement();

            

            int result = statement

                    .executeUpdate("insert into student values(30,'zhaoyun',99,100)");

            if (result > 0) {

                System.out.println("插入成功");

            } else {

                System.out.println("插入失败");

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    @Test

    public void remove() {

        //删除语句

        try {

            Class.forName("com.mysql.jdbc.Driver");

            Connection connection = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/lol", "root", "1q2w3e");

            Statement statement = connection.createStatement();

            int result = statement

                    .executeUpdate("delete from student where id > 20");

            if (result > 0) {

                System.out.println("删除成功" + result);

            } else {

                System.out.println("删除失败" + result);

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    @Test

    public void update() {

        //更新语句

        try {

            Class.forName("com.mysql.jdbc.Driver");

            Connection connection = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/lol", "root", "1q2w3e");

            Statement statement = connection.createStatement();

            int result = statement

                    .executeUpdate("update student set english = 100 where id = 30");

            if (result > 0) {

                System.out.println("更新成功" + result);

            } else {

                System.out.println("更新失败" + result);

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    @Test

    public void query() {

        //查询语句

        try {

            Class.forName("com.mysql.jdbc.Driver");

            Connection connection = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/lol", "root", "1q2w3e");

            Statement statement = connection.createStatement();

            ResultSet resultSet = statement

                    .executeQuery("select * from student");

            while (resultSet.next()) {

                String id = resultSet.getString(1);

                String name = resultSet.getString(2);

                String english = resultSet.getString(3);

                String math = resultSet.getString(4);

                

                System.out.println("id = "+id +" name = "+name + " english = "+english +" math = "+math);

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

4,JDBC的封装:

    [1]创建配置文件:db.properties 存放路径,帐号,密码。

className:com.mysql.jdbc.Driver

url:jdbc:mysql://localhost:3306/lol

name:root

pwd:1q2w3e

    [2] 创建Utils getConnection获取对数据库的链接

public class JDBCUtils {

    private static String className;

    private static String url;

    private static String name;

    private static String pwd;

    static {

        try {

            //保存本地配置

            Properties properties = new Properties();

            properties.load(new FileInputStream("db.properties"));

            //获取properties 中的字段

            className = properties.getProperty("className");

            url = properties.getProperty("url");

            name = properties.getProperty("name");

            pwd = properties.getProperty("pwd");

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    //获取

    public static Connection getConnection() throws Exception {

        Class.forName(className);

        return DriverManager.getConnection(url, name, pwd);

    }

}

    [3] 写SQL语句

public class JDBCUtilsTest {

    @Test

    public void add() {

        try {

            Connection connection = JDBCUtils.getConnection();

            Statement statement = connection.createStatement();

            int result = statement.executeUpdate("insert into student values(30,'machao',99,99)");

            System.out.println("result = "+result);

            

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        };

    }

    @Test

    public void remove() {

        try {

            Connection connection = JDBCUtils.getConnection();

            Statement statement = connection.createStatement();

            int result = statement.executeUpdate("delete from student where id = 30");

            System.out.println("result = "+result);

            

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        };

    }

    @Test

    public void update() {

        try {

            Connection connection = JDBCUtils.getConnection();

            Statement statement = connection.createStatement();

            int result = statement.executeUpdate("update student set english = 100 where id =20");

            System.out.println("result = "+result);

            

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        };

        

    }

    @Test

    public void query() {

        try {

            Connection connection = JDBCUtils.getConnection();

            Statement statement = connection.createStatement();

            

            ResultSet resultSet = statement.executeQuery("select * from student");

            

            while(resultSet.next()){

                String id = resultSet.getString(1);

                String name = resultSet.getString(2);

                String english = resultSet.getString(3);

                String math = resultSet.getString(4);

                System.out.println("id = "+id +" name = "+name + " english = "+english  +" math = "+math);

            }

            

            

            

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        

    }

}

5,login登陆查询数据库

    *BUG,预编译。占位符。进行编译转义。

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/83753941