• JDBC


     

     

            Connection conn=DriverManager.getConnection(url,user,password);  //上面那句写错了

     

     

     

     

     conn和stmt的声明要写在try的外面

    第四步中的Sql查询语句怎么写?

     

     

     

    import java.sql.*;
    
    public class JDBCTest {
        public static void main(String[] args) {
            Statement stmt=null;
            Connection conn=null;
            ResultSet rs=null;
            try {
                //1.注册驱动
    //            Driver driver=new com.mysql.cj.jdbc.Driver();     //注册驱动方式一
    //            DriverManager.registerDriver(driver);
                Class.forName("com.mysql.cj.jdbc.Driver");          //注册驱动方式二
                //2.数据库连接
                String url="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
                String user="root";
                String pwd="1234";
                conn=DriverManager.getConnection(url,user,pwd);
                //3.获取数据库操作对象
                stmt=conn.createStatement();
                //4.执行sql语句
                String sql="select * from user";
                rs=stmt.executeQuery(sql);
                //5.对查询结果进行处理
                while(rs.next()){
                    String username=rs.getString("username");
                    String sex=rs.getString("sex");
                    System.out.println(username+","+sex);
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                //6.释放资源
                if(rs!=null){
                    try {
                        rs.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(stmt!=null){
                    try {
                        stmt.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(conn!=null){
                    try {
                        conn.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    使用配置文件来连接数据库,先创建一个db.properties配置文件,里面输入driver、user、pwd、url等信息

    如图:

    然后使用ResourceBundle读取配置文件中的信息

    import java.sql.*;
    import java.util.ResourceBundle;
    
    public class JDBCTest {
        public static void main(String[] args) {
            ResourceBundle bundle=ResourceBundle.getBundle("resources/db");
            String driver=bundle.getString("driver");
            String url=bundle.getString("url");
            String user=bundle.getString("user");
            String pwd=bundle.getString("pwd");
            Statement stmt=null;
            Connection conn=null;
            ResultSet rs=null;
            try {
                //1.注册驱动
    //            Driver driver=new com.mysql.cj.jdbc.Driver();     //注册驱动方式一
    //            DriverManager.registerDriver(driver);
                Class.forName(driver);          //注册驱动方式二
                //2.数据库连接
    //            String url="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
    //            String user="root";
    //            String pwd="1234";
                conn=DriverManager.getConnection(url,user,pwd);
                //3.获取数据库操作对象
                stmt=conn.createStatement();
                //4.执行sql语句
                String sql="select * from user";
                rs=stmt.executeQuery(sql);
                //5.对查询结果进行处理
                while(rs.next()){
                    String username=rs.getString("username");
                    String sex=rs.getString("sex");
                    System.out.println(username+","+sex);
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                //6.释放资源
                if(rs!=null){
                    try {
                        rs.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(stmt!=null){
                    try {
                        stmt.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(conn!=null){
                    try {
                        conn.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

     

    ////运用PreparedStatement解决sql注入问题
    import java.sql.*;
    import java.util.ResourceBundle;
    
    public class JDBCTest {
        public static void main(String[] args) {
            ResourceBundle bundle=ResourceBundle.getBundle("resources/db");
            String driver=bundle.getString("driver");
            String url=bundle.getString("url");
            String user=bundle.getString("user");
            String pwd=bundle.getString("pwd");
            PreparedStatement stmt=null;
            Connection conn=null;
            ResultSet rs=null;
            try {
                //1.注册驱动
    //            Driver driver=new com.mysql.cj.jdbc.Driver();     //注册驱动方式一
    //            DriverManager.registerDriver(driver);
                Class.forName(driver);          //注册驱动方式二
                //2.数据库连接
    //            String url="jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
    //            String user="root";
    //            String pwd="1234";
                conn=DriverManager.getConnection(url,user,pwd);
                //3.获取数据库操作对象
                //?表示占位符,一个占位符只能接收一个值/数据
                String sql="select * from user where username=?";   //主义?不要加单引号
                stmt=conn.prepareStatement(sql); //此时会发送sql给DBMS,进行sql语句编译
                //给占位符?传值,JDBC中所有下标都是从1开始
                //怎么解决sql注入的?在往sql语句中传递参数 的时候,sql语句以及被编译过了
                stmt.setString(1,"小二王");    //第一个参数表示占位符的序号(从1开始),第二个是实参
                // 4.执行sql语句,此时执行的时候不需要传入sql语句了
                rs=stmt.executeQuery();
                //5.对查询结果进行处理
                while(rs.next()){
                    String username=rs.getString("username");
                    String sex=rs.getString("sex");
                    System.out.println(username+","+sex);
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                //6.释放资源
                if(rs!=null){
                    try {
                        rs.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(stmt!=null){
                    try {
                        stmt.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
                if(conn!=null){
                    try {
                        conn.close();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    [二、实用控件]12使用Map视图创建地图和MapPin
    [二、实用控件]17在UIKit项目中使用Swift UI里的视图
    [二、实用控件]19将颜色、渐变、图片和图形作为视图的背景
    [二、实用控件]15使用定时器实现环形进度条的定时隐藏
    [二、实用控件]18利用旋转和偏移功能对视图进行镜像操作
    [二、实用控件]13使用MapKit里的地图视图
    [二、实用控件]20通过AnyView返回人意类型的视图
    [二、实用控件]14使用UIKit里的环形进度条
    [二、实用控件]16使用ProgressView快速创建环形进度条和水平进度条
    [二、实用控件]21使用ViewModifier视图修饰符集成多个样式
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/14236360.html
Copyright © 2020-2023  润新知