ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

使用JDBC操作数据库

2022-06-26 12:03:23  阅读:209  来源: 互联网

标签:JDBC rs 数据库 System sql println 操作 con out


jdbc访问数据库步骤

  1、Class.forName()加载驱动

  2、DriverManager获取Connection连接

  3、创建Statement执行SQL语句

  4、返回ResultSet查询结果

  5、释放资源

public class DBUtils {
    public static Connection getCon() {
        Connection con = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
            String user = "root";
            String password = "123456";
            con = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(ResultSet rs,Statement st ,Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

        Connection con = DBUtils.getCon();
        Statement st = con.createStatement();
        //Statement会有sql注入风险
        String sql = "select * from t_user where usname= '" + usname + "' and psword ='" + psword + "'";
        System.out.println("sql:"+sql);
        ResultSet rs = st.executeQuery(sql);
        if(rs.next()) {
            System.out.println("登录成功!");
            System.out.println(rs.getInt(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
            System.out.println(rs.getInt(4));
            System.out.println(rs.getString(5));
        }else {
            System.out.println("用户名或密码不正确,请重新输入!");
        }

 

 

Connection接口常用方法

 

 

Statement接口

  Statement 是 Java 执行数据库操作的一个重要接口,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。Statement对象,用于执行不带参数的简单SQL语句。

 

 

ResultSet

  数据查询操作会利用SQL语句向数据库发出SELECT查询指令,而查询的结果如果要返回给程序进行处理,就必须通过ResultSet接口来进行封装,ResultSet是一种可以保存任意查询结果的集合结构,所有查询结果会通过ResultSet在内存中形成一张虚拟表的形式,而后开发者可以根据数据行的索引,依照数据类型获取列数据内容

 

 

PreparedStatement

  继承自statement

  preparedStatement防止sql注入的方式是把用户非法输入的字符用\反斜杠做了转义,从而达到了防止sql注入的目的

public class Controller1 {

    public static void main(String[] args) throws Exception {
        //1.PreparedStatement和Statement啥关系?
        //2.为啥要用这个玩意儿?2.1防止SQL注入2.2预编译能提升效率
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String usname = sc.nextLine();
        System.out.println("请输入密码:");
        String psword = sc.nextLine();

        Connection con = DBUtils.getCon();
        String sql = "select * from t_user where username= ? and password =?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, usname);
        pst.setString(2, psword);
        ResultSet rs = pst.executeQuery();
        if(rs.next()) {
            System.out.println("登录成功!");
            System.out.println(rs.getInt(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
            System.out.println(rs.getInt(4));
            System.out.println(rs.getString(5));
        }else {
            System.out.println("用户名或密码不正确,请重新输入!");
        }
        
        DBUtils.close(rs, pst, con);
    }
}

标签:JDBC,rs,数据库,System,sql,println,操作,con,out
来源: https://www.cnblogs.com/jiaxin30/p/16413243.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有