当前位置:Gxlcms > 数据库问题 > JDBC入门

JDBC入门

时间:2021-07-01 10:21:17 帮助过:5人阅读

com.test; import java.sql.*; public class JDBC01 { public static void main(String[] args) { ResultSet rs = null; Connection connection = null; PreparedStatement statement = null; try { //1,加载驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.创建连接 //此处按照实际的数据库名称和账号密码进行修改 //格式为jdbc:mysql://127.0.0.1:3306/数据库名称?useSSL=true&characterEncoding=utf-8&user=账号名&password=密码 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8&user=root&password=123456&serverTimezone=UTC"); System.out.println("创建连接成功"); //3.写sql //根据数据库实际的表名写SQL语句 String sql="select * from pet"; //4.得到statement对象执行sql statement = connection.prepareStatement(sql); //5.得到结果集 rs = statement.executeQuery(); //6.处理结果集 while(rs.next()){ System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); System.out.println(rs.getString(3)); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { //7.关闭 if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement!=null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } System.out.println("关闭成功"); } } }

 

第一次接触sql注入问题,尝试一下学校的教务网站,好像被查水表了。

//输入password为123‘ or ‘1‘=‘1
select * from users where username=123 or 1=1 and password=123 or 1=1’;

 

技术图片

 

JDBC入门

标签:cut   get   png   main   一个   odi   cto   manage   one   

人气教程排行