数据库连接
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true"; |
| String username= "root"; |
| String password = "123456"; |
| |
| |
| Connection connection = DriverManager.getConnection(url,username,password); |
| |
| Statement statement = connection.createStatement(); |
| |
| |
| |
| |
| |
| String sql="select * from user"; |
| ResultSet resultSet = statement.executeQuery(sql); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| while (resultSet.next()){ |
| System.out.println("id=" +resultSet.getObject("id")); |
| System.out.println("name=" +resultSet.getObject("name")); |
| System.out.println("age=" +resultSet.getObject("age")); |
| |
| |
| } |
| |
| |
| resultSet.close(); |
| statement.close(); |
| connection.close(); |
| |
utils 工具类
| public class JdbcUtils { |
| private static final String driver = "com.mysql.cj.jdbc.Driver"; |
| private static final String username = "root"; |
| private static final String password = "123456"; |
| private static final String url = "jdbc:mysql://localhost:3306/rbac?useUnicode=true&&characterEncoding=utf-8"; |
| |
| static { |
| try { |
| |
| Class.forName(driver); |
| |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| public static Connection getConnection() throws SQLException { |
| return DriverManager.getConnection(url, username, password); |
| |
| } |
| |
| |
| public static void release(Connection conn, Statement st, ResultSet rs) { |
| if (rs != null) { |
| try { |
| rs.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| if (st != null) { |
| try { |
| st.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| } |
| if (conn != null) { |
| try { |
| conn.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| } |
| } |
数据库CROD
| public class Testupdate { |
| public static void main(String[] args) { |
| Connection conn=null; |
| Statement st=null; |
| ResultSet rs=null; |
| try { |
| conn= JdbcUtils.getConnection(); |
| st=conn.createStatement(); |
| |
| String sql="update employee set name='anie2' where name='annie'"; |
| |
| int i=st.executeUpdate(sql); |
| if (i>0){ |
| System.out.println("update 成功"); |
| |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| }finally { |
| JdbcUtils.release( conn,st,rs); |
| |
| } |
| |
| |
| } |
| |
| } |
防止sql 注入
| public static void main(String[] args) { |
| |
| login(" '' or 1=1 " , "22"); |
| |
| } |
| public static void login(String username ,String password){ |
| Connection conn=null; |
| PreparedStatement st= null; |
| ResultSet rs=null; |
| try { |
| conn = JdbcUtils.getConnection(); |
| String sql="select * from user where 'name =? and 'password' =? "; |
| |
| st =conn.prepareStatement(sql); |
| st.setString(1, username); |
| st.setString(2, password); |
| |
| rs = st.executeQuery(); |
| |
| |
| |
| |
| |
| while (rs.next()){ |
| System.out.println(rs.getString("name")); |
| System.out.println(rs.getString("password")); |
| } |
| |
| } catch (SQLException e) { |
| throw new RuntimeException(e); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术