jdbc连接mysql数据库增删_Java之简单的jdbc连接MySQL数据库实现增删改查

news/2024/7/18 4:04:22 标签: jdbc连接mysql数据库增删

/**

* 1.加载mysql驱动:com.mysql.jdbc.Driver

* 2.获取连接:与数据库建立连接

* 3.sql预编译对象:预编译对象

* 4.执行sql语句

* 5.释放资源

* @author 郑清

*/

public class JDBC_CRUD_Demo {

public static void main(String[] args) {

testCreateDatabase();//创建数据库

add();//添加数据

delete();//删除数据

update();//修改数据

select();//查找数据

}

public static void testCreateDatabase() {

Connection connection = null;

Statement statement = null;

try {

//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接:与数据库建立连接

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");

//3.获得sql的预编译对象

statement = connection.createStatement();

//4.执行sql语句 ex:创建表t_user

String sql = "CREATE TABLE t_user("

+" `id` int(11) NOT NULL,"

+" `username` varchar(20) NOT NULL,"

+" `password` int(11) NOT NULL,"

+" PRIMARY KEY (`id`)"

+" ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

statement.executeUpdate(sql);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//5.释放资源 (注意:关闭资源顺序 先打开后关闭)

try {

if(statement!=null) {

statement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(connection!=null) {

connection.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

public static void add() {

Connection connection = null;

Statement statement = null;

try {

//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接:与数据库建立连接

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");

//3.获得sql的预编译对象

statement = connection.createStatement();

//4.执行sql语句 ex:插入数据到表t_user

String sql = "insert into t_user(id,username,password) values(2,'张三','123'),(10,'李四','123456');";

statement.executeUpdate(sql);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//5.释放资源 (注意:关闭资源顺序 先打开后关闭)

try {

if(statement!=null) {

statement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(connection!=null) {

connection.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

public static void delete() {

Connection connection = null;

Statement statement = null;

try {

//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接:与数据库建立连接

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");

//3.获得sql的预编译对象

statement = connection.createStatement();

//4.执行sql语句 ex:删除表t_user中的数据

String sql = "delete from t_user where id = 2;";

statement.executeUpdate(sql);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//5.释放资源 (注意:关闭资源顺序 先打开后关闭)

try {

if(statement!=null) {

statement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(connection!=null) {

connection.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

public static void update() {

Connection connection = null;

Statement statement = null;

try {

//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接:与数据库建立连接

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");

//3.获得sql的预编译对象

statement = connection.createStatement();

//4.执行sql语句 ex:修改表t_user中数据

String sql = "update t_user set username='修改名字',password='111111' where id=3;";

statement.executeUpdate(sql);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//5.释放资源 (注意:关闭资源顺序 先打开后关闭)

try {

if(statement!=null) {

statement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(connection!=null) {

connection.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

public static void select() {

Connection connection = null;

Statement statement = null;

ResultSet rs = null;

try {

//1.加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2.获取连接:与数据库建立连接

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhengqing_test", "root", "root");

//3.获得sql的预编译对象

statement = connection.createStatement();

//4.执行sql语句 ex:查找表t_user中数据

//查找id为3的一条数据

/*String sql = "select * from t_user where id = 3;";

rs = statement.executeQuery(sql);

System.out.println(rs.next());

System.out.println(rs.getInt("id"));

System.out.println(rs.getString("username"));

System.out.println(rs.getString("password"));*/

//查找表中全部数据

String sql = "select * from t_user;";

rs = statement.executeQuery(sql);

while(rs.next()){

System.out.print(rs.getInt("id")+"-->");

System.out.print(rs.getString("username")+"-->");

System.out.println(rs.getString("password"));

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//5.释放资源 (注意:关闭资源顺序 先打开后关闭)

try {

if(rs!=null)rs.close();

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(statement!=null)statement.close();

} catch (SQLException e) {

e.printStackTrace();

}finally{

try {

if(connection!=null)connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

}


http://www.niftyadmin.cn/n/629161.html

相关文章

计算机技术及应用研究,计算机信息技术发展及应用研究

计算机信息技术的应用和发展在信息社会中有着极为重要的意义。它影响和指导着我们的生活,不论是工厂生产还是高校管理,不论是物流传输还是办公自动化,都默默地发生着改变,计算机技术使人们降低了劳动强度,提高了工作效…

yii2 连接mysql_Yii2中配置使用连接多个数据库连接(config Model)

在使用Yii2开发的一些项目中需要调用多个数据库,这个时候我们就需要在model里面和在配置文件里面增加一些方法,使我们的model类能直接便捷的切换不同的数据库。在你的配置文件下面新建一个远程数据库链接的配置文件。例如 config/remote_db.phpreturn [c…

XXL-RPC v1.3.2,分布式服务框架

Release Notes 1、泛化调用:服务调用方不依赖服务方提供的API;2、新增通讯方案 "NETTY_HTTP";3、新增序列化方案 "KRYO";4、通讯效率优化:TCP连接池取消,改为单一长连接,移…

mysql c 重连 字符集_Mysql 字符集 设置

近日,由这折腾了一下Mysql的中文配置,一直想找一种简单的方法来处理在B/S的问题。尽管全站统一为utf-8编码时,在数据库和连接字符集都使用latin1时虽然大部分情况下都可以解决乱码问题,但缺点是无法以字符为单位来进行…

HTML glop打印插件,超级方便的万能容器Glop,可以捏成任何形状

原标题:超级方便的万能容器Glop,可以捏成任何形状更多精彩内容访问:Glop是由西班牙设计师berta juli sala设计的,可以作为杯子、碗、盘子、瓶子、搅拌盘或是任何你能够想到的东西来使用。因为Glop质地非常柔软,使用者可…

面试常见问题_外企制造业项目管理面试常见问题及对策答复

各位知乎友,基于最近收到的很多的咨询,下面是基于过往面试的问题做了更新(斜体部分),基本上cover 了所有的外企面试套路,在行业内从小小项目工程师混到负责亚洲的项目管理工作,下面是一些常见问…

ios mysql注册登录界面_iOS+PHP注册登录系统 PHP部分(上)

最后达成效果:分析做项目的基本流程:1.先创建数据库2.写PHP服务端3.写iOS用户端1.创建数据库我用的是wamp。数据库名称为ioslogin,表名为users其中有3个字段user_id,user_name和user_pwd。user_id为自增长。2.php服务端php端我用的是EclipsePHP编写。先创…

微信指纹支付原理浅析

微信指纹支付从设备出厂到完成支付大致可以分成4个步骤、3对公私钥:1.设备出厂公私钥及tee适配手机厂商在手机tee的RPMB区域内置私钥A_priv及微信支付的特定ta和SoterKeyStore,然后厂商统一将该设备信息和设备公钥A_pub存储在腾讯服务器(校验…