1、参考
MySQL基础
mysql-connector-java-5.1.40.zip
2、代码
| public class JDBCDemo01 { |
| public static void main(String[] args) throws ClassNotFoundException, SQLException { |
| |
| |
| |
| Class.forName("com.mysql.jdbc.Driver"); |
| |
| |
| String url = "jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&useSSL=false"; |
| String userName = "root"; |
| String passWord = "root"; |
| |
| Connection connection = DriverManager.getConnection(url, userName, passWord); |
| |
| Statement statement = connection.createStatement(); |
| |
| String sql = "SELECT * FROM users"; |
| |
| 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("password=" + resultSet.getObject("password")); |
| System.out.println("email=" + resultSet.getObject("email")); |
| System.out.println("birthday=" + resultSet.getObject("birthday")); |
| System.out.println("==============================="); |
| } |
| |
| resultSet.close(); |
| statement.close(); |
| connection.close(); |
| } |
| } |
3、JDBCUtils
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.sql.Connection; |
| import java.sql.DriverManager; |
| import java.sql.ResultSet; |
| import java.sql.SQLException; |
| import java.sql.Statement; |
| import java.util.Properties; |
| |
| public class JDBCUtils { |
| private static String driver = null; |
| private static String url = null; |
| private static String username = null; |
| private static String password = null; |
| |
| static { |
| try { |
| InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"); |
| Properties properties = new Properties(); |
| properties.load(in); |
| |
| driver = properties.getProperty("driver"); |
| url = properties.getProperty("url"); |
| username = properties.getProperty("username"); |
| password = properties.getProperty("password"); |
| |
| |
| Class.forName(driver); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (ClassNotFoundException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| |
| |
| public static Connection getConnection() throws SQLException { |
| return DriverManager.getConnection(url, username, password); |
| } |
| |
| |
| |
| |
| public static void release(Connection con, 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 (con != null) { |
| try { |
| con.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
db.properties
| driver=com.mysql.jdbc.Driver |
| url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&useSSL=false |
| username=root |
| password=root |
4、sql注入漏洞解决
| public class TestInsert { |
| public static void main(String[] args) { |
| Connection con = null; |
| PreparedStatement st = null; |
| ResultSet rs = null; |
| try { |
| con = JDBCUtils.getConnection(); |
| |
| String sql = "INSERT INTO users(`id`,`name`,`password`,`email`,`birthday`) VALUES (?,?,?,?,?)"; |
| |
| st = con.prepareStatement(sql); |
| |
| st.setInt(1, 5); |
| st.setString(2, "钱七"); |
| st.setString(3, "123456"); |
| st.setString(4, "qianqi@sina.com"); |
| st.setDate(5, new java.sql.Date(new java.util.Date().getTime())); |
| int num = st.executeUpdate(); |
| if (num > 0) { |
| System.out.println("插入成功!"); |
| } |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } finally { |
| JDBCUtils.release(con, st, rs); |
| } |
| |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了