Java JDBC篇2——JDBC增删查改
2024-05-13 17:17:38
Java JDBC篇2——JDBC增删查改
url=jdbc:mysql://localhost:3306/test
user=root
password=blingbling123.
driver=com.mysql.jdbc.Driver
public class JDBCtool {private static String urls;private static String user;private static String password;private static String driver;static {Properties properties=new Properties();ClassLoader classLoader=JDBCtool.class.getClassLoader();URL url=classLoader.getResource("connection.properties");String path=url.getPath();try {properties.load(new FileReader(path));} catch (IOException e) {e.printStackTrace();}urls=properties.getProperty("url");user=properties.getProperty("user");password=properties.getProperty("password");driver=properties.getProperty("driver");try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getconnection() throws SQLException {return DriverManager.getConnection(urls,user,password);}public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){if (resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (preparedStatement!=null){try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}
}
1、插入
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="INSERT INTO USER (username, PASSWORD,birthday) VALUES('admin1', '123','2000-12-24')";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);}
}
2、查询
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="select * from user";PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}JDBCtool.close(connection,preparedStatement,resultSet);}
}
3、更新
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="update user set username='yoya' where id=1";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);}
}
4、删除
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="delete from user where id in (4,5)";PreparedStatement preparedStatement = connection.prepareStatement(sql);System.out.println(preparedStatement.executeUpdate());JDBCtool.close(connection,preparedStatement,null);}
}
5、防止SQL注入
public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="select * from user where id=?";PreparedStatement preparedStatement = connection.prepareStatement(sql);preparedStatement.setInt(1,1);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){System.out.println(resultSet.getString("username"));}JDBCtool.close(connection,preparedStatement,resultSet);}
}
Java JDBC篇2——JDBC增删查改相关推荐
- java调用js查询mongo_MongoDB增删查改操作示例【基于JavaScript Shell】
本文实例讲述了MongoDB增删查改操作.分享给大家供大家参考,具体如下: MongoDB自带了一个JavaScript Shell,所以在其中使用js语法是可以的. Insert操作: 单条插入 v ...
- 粤嵌星计划打卡第90天(JDBC对于数据库中增删查改的操作)
#粤嵌我来了##粤嵌星计划# 粤嵌星计划挑战 今天打卡第90天
- 用java项目做Hibernate的增删查改,和分页(hibernate-distribution-3.6.0.Final为例)
1, 在文档中找到导入7个jar包 hibernate3.jar 核心jar包 lib-required下的全部jar包6个 lib-jpa下的一个jar 2,写一个配置文件config 默认名为hi ...
- 【JAVA数据结构】双向链表的增删查改
春水初生,春林初盛,春风十里,不如你 大家好,这里是新一,请多关照
- 【JAVA数据结构】链表的增删查改(单向不带头非循环链表)
月色与雪色之间,你是第三种绝色 大家好,这里是新一,请多关照
- 支持增删查改的简单Java Web通讯录详细教程【基于Mac OS+IDEA+Servlet+JDBC+Tomcat】
本文将以Web版通讯录的形式介绍一个支持简单增删查改的Java Web项目,软硬件环境基于Macbook Air M2+macOS Ventura 13.1+IntelliJ IDEA 2022,技术 ...
- 安卓后端mysql_后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)
1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...
- Java、SQL Serve ----简单的增删查改
--创建 Student表 CREATE TABLE Student(Sno CHAR(9) PRIMARY KEY, Sname CHAR(20) NOT NULL, Ssex CHAR(2),Sa ...
- 使用java图形界面实现简单的数据库增删查改
1.确定数据库连接类型 这里我使用的是SqlServer数据库 如果需要修改数据库的连接类型,可以自己修改数据库的连接 package com.andios.ForFunction;import ja ...
最新文章
- ajax 没有权限 -quot;跨域quot;,如何解决AJAX中跨域访问出现'没有权限'的错误
- [学习笔记]几个英语短句(1)
- .NET实现应用程序登录Web页
- 【数据结构与算法】之深入解析“二叉树展开为链表”的求解思路与算法示例
- Linq入门演练---(1)基本用法-分组,排序,内连接
- 如果有一天生你养你的两个人都走了
- MFC 获取其他窗口的Edit文本和单击Button
- Windows开机自动运行软件目录
- linux内核的编译
- 小程序获取用户信息 php发送数据库,qq小程序如何获取用户信息并存入数据库实例...
- AppStore_隐私政策
- 用android编写使用按钮ImageButton和切换器ImageSwitcher
- VPX单板计算机xPower 6110
- 格雷码与二进制之间的转换
- 使用HTTP下载单词音标,特此记录
- 天池大数据竞赛平台-东电网智慧现场作业挑战赛:识别高空作业及安全带佩戴Baseline(非官方)
- 国内十大白银期货APP最新排名
- Java常量池[乐乐独记]
- Sonatype Nexus: Recommended file descriptor limit is 65536 but count is 4096
- 还不会做简历?Word自带功能,1秒自动生成简历!
热门文章
- Python中的__init__和self是做什么的?
- docker集群搭建(k8s)
- java 类数组_Java常用类-字符串、日期类、算法及数组工具类等
- synchronized 中的 4 个优化,你知道几个?
- 解决exe文件在别人电脑上运行缺失文件情况
- Django创建mysql数据库常用字段及参数
- Ubuntu下安装配置VIM/GVIM(GUI-Vim)
- Xftp上传文件显示状态错误
- RequestDispatcher提供两个方法:forward,include有什么区别
- php excel header,【IT专家】PHP生成excel,方法一-header生成