JDBC连接数据库

 

一.使用statement进行jdbc数据库查询

  Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可。
  Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sql语句,executeUpdate执行完后,将会返回一个整数(即增删改语句导致了数据库几行数据发生了变化)。
  Statement.executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。

(1)查询---read

public class JdbcDemo {

    public static void main(String[] args) throws Exception {
        //要连接的数据库URL
        String url = "jdbc:mysql://localhost:3306/test";
        //连接的数据库时使用的用户名
        String username = "root";
        //连接的数据库时使用的密码
        String password = "root";

        //1.加载驱动
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());不推荐使用这种方式来加载驱动
        Class.forName("com.mysql.jdbc.Driver");//推荐使用这种方式来加载驱动
        //2.获取与数据库的链接
        Connection conn = DriverManager.getConnection(url, username, password);

        //3.获取用于向数据库发送sql语句的statement
        Statement st = conn.createStatement();

        String sql = "select id,name,age,email,role,phone from user";
        //4.向数据库发sql,并获取代表结果集的resultset
        ResultSet rs = st.executeQuery(sql);

        //5.取出结果集的数据
        while(rs.next()){
            System.out.println("id=" + rs.getObject("id"));
            System.out.println("name=" + rs.getObject("name"));
            System.out.println("password=" + rs.getObject("age"));
            System.out.println("email=" + rs.getObject("email"));
            System.out.println("birthday=" + rs.getObject("role"));
            System.out.println("birthday=" + rs.getObject("phone"));
        }

        //6.关闭链接,释放资源
        rs.close();
        st.close();
        conn.close();
    }
}

(2)插入--create

 Statement st = conn.createStatement();
 String sql = "insert into user(….) values(…..) "; 
 int num = st.executeUpdate(sql);
 if(num>0){
     System.out.println("插入成功!!!");
 }

(3)删除--delete

 Statement st = conn.createStatement();
 String sql = “delete from user where id=1; 
 int num = st.executeUpdate(sql);
 if(num>0){
     System.out.println(“删除成功!!!");
 }

(4)更新--update

 Statement st = conn.createStatement();
 String sql = “update user set name=‘’ where name=‘’"; 
 int num = st.executeUpdate(sql);
 if(num>0){
     System.out.println(“修改成功!!!");
 }

二.使用PreparedStatement进行jdbc数据库查询

  PreperedStatement是Statement的子类,它的实例对象可以通过调用Connection.preparedStatement()方法获得,相对于Statement对象而言:PreperedStatement可以避免SQL注入的问题。
  Statement会使数据库频繁编译SQL,可能造成数据库缓冲区溢出。PreparedStatement可对SQL进行预编译,从而提高数据库的执行效率。并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写。

public class javaJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException{
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1、加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、通过驱动管理类获取数据库链接
            connection =  DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "root");
            //3、定义sql语句 ?表示占位符
            String sql = "select * from user where name = ?";
            //4、获取预处理statement
            preparedStatement = connection.prepareStatement(sql);
            //5、设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
            preparedStatement.setString(1, "戈香怡");
            //6、向数据库发出sql执行查询,查询出结果集
            resultSet =  preparedStatement.executeQuery();
            //7、遍历查询结果集
            while(resultSet.next()){
                System.out.println(resultSet.getString("id")+"  "+resultSet.getString("role"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //8、释放资源
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
}
posted @ 2018-08-23 14:32  何其小静  阅读(246)  评论(0编辑  收藏  举报