连接mysql增删改查

新建 Directory 名称为lib
在这里插入图片描述
复制 黏贴 选中lib右击 add as Library
在这里插入图片描述
ok
在这里插入图片描述
查询
package com.zhongruan;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
Connection connection =DriverManager.getConnection( “jdbc:mysql://localhost/zjgm?user=root&password=123456”);
System.out.println(“创建连接成功”);
//3.写sql
String sql=“select * from stu”;
//4.得到statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//5.执行sql
ResultSet resultSet = statement.executeQuery();
//6.处理结果集
while (resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getNString(2);
int age =resultSet.getInt(3);
System.out.println(id+name+age);
}
//7.关闭资源
resultSet.close();
statement.close();
connection.close();
}
}

删除
package com.zhongruan;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);

    Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
    System.out.println("创建连接成功");

   // String sql="select * from stu";
    String sql="delete from stu where id=1";

    PreparedStatement statement = connection.prepareStatement(sql);

statement.executeUpdate();

// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}

添加

package com.zhongruan;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);

    Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
    System.out.println("创建连接成功");

   // String sql="select * from stu";
    String sql="insert into stu(id,name,age) values ('1','haha',18)";
    //insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
    PreparedStatement statement = connection.prepareStatement(sql);

statement.executeUpdate();

// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}

修改

package com.zhongruan;

import java.sql.*;

public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(“com.mysql.jdbc.Driver”);

    Connection connection =DriverManager.getConnection( "jdbc:mysql://localhost/zjgm?user=root&password=123456");
    System.out.println("创建连接成功");

   // String sql="select * from stu";
    String sql="update stu set age=12 where name='haha'";
   // update student set money=110 where name='hk';
    //insert into student (name,money,sex,phone) values ('hk',10000,'男',188);
    PreparedStatement statement = connection.prepareStatement(sql);

statement.executeUpdate();

// while (resultSet.next()){
// int id=resultSet.getInt(1);
// String name=resultSet.getNString(2);
// int age =resultSet.getInt(3);
// System.out.println(id+name+age);
// }
//resultSet.close();
statement.close();
connection.close();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值