1. 程式人生 > >SQL增刪改查、SQL注入、事務

SQL增刪改查、SQL注入、事務

SQL注入:
SELECT * FROM user WHERE name=”abcd” and password=”“;
當password設定為1234” or “1”=”1”;
SELECT * FROM user WHERE name=”abcd” and password=”1234” or “1”=”1”;
所以在JDBC中,使用?替代引數

事務寫法:

將獲取到的連線的自動提交關閉就OK
Connection con=...;
con.setAutoConnit(false);
//操作完成之後再提交
con.commit;

JavaBean建立

public class
Student { private Integer Id; private String Name; private Integer Cno; public Integer getId() { return Id; } public void setId(Integer id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public
Integer getCno() { return Cno; } public void setCno(Integer cno) { Cno = cno; } @Override public String toString() { return "Student [Id=" + Id + ", Name=" + Name + ", Cno=" + Cno + "]"; } }

資料庫資源封裝:

public class DataSource {
    public static final
String DRIVER = "com.mysql.jdbc.Driver"; public static final String URL = "jdbc:mysql://192.168.216.3:3306/eclipse?useUnicode=true&characterEncoding=utf8"; public static final String USER = "root"; public static final String PASSWORD = "123456"; } //我的資料庫在虛擬機器裡,埠號和賬號密碼如上

增刪改查封裝:

public class jdbcutil {
    public static Connection getConnection() {
        Connection con = null;
        try {
            Class.forName(util.DataSource.DRIVER);
            con = DriverManager.getConnection(util.DataSource.URL, util.DataSource.USER, util.DataSource.PASSWORD);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }

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

    public static int executeUpdate(String sql, Object... params) {
        int result = 0;
        Connection con = getConnection();
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            util.jdbcutil.closeAll(null, pstmt, con);
        }
        return result;
    }

    public static <T> List<T> executeQuery(String sql, RowMap<T> rowmap, Object... params) {
        Connection con = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<T> result = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i + 1, params[i]);
                }
            }
            rs = pstmt.executeQuery();
            while (rs.next()) {
                T t = rowmap.RowMapping(rs);
                result.add(t);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(rs, pstmt, con);
        }
        return result;
    }
}

介面定義:

public interface RowMap<T> {
    public T RowMapping(ResultSet rs);
}