jdbc对mysql的增删改查语句_jdbc方式mysql语句增删改查

import java.sql.*;

public class CRUD {

public static void main(String[] args) throws SQLException {

select();

System.out.println("-------------------------");

insert();

select();

System.out.println("-------------------------");

delete();

select();

System.out.println("-------------------------");

update();

select();

System.out.println("-------------------------");

}

public static void select() throws SQLException {

Connection connection = null;

PreparedStatement preparedStatement = null;

ResultSet resultSet = null;

DBUtil dbUtils = new DBUtil();

try {

connection = dbUtils.getConnection();

String sql = "select * from studentinfo";

preparedStatement = connection.prepareStatement(sql);

resultSet = preparedStatement.executeQuery();

// System.out.println("=================");

while (resultSet.next()) {

System.out.println(resultSet.getInt("student_id") + " " + resultSet.getString("student_name"));

}

// dbUtils.closeDBResouse(connection, preparedStatement, resultSet);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

public static void insert() {

DBUtil dbUtil = new DBUtil();

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

conn = dbUtil.getConnection();

String sql = "insert into studentinfo (student_id,student_name,student_pwd) values (1,'wwwwww','1233')";

stmt = conn.prepareStatement(sql);

int count = stmt.executeUpdate(sql);

System.out.println("插入了" + count + "条语句");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void delete() {

DBUtil dbUtil = new DBUtil();

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

conn = dbUtil.getConnection();

String sql = "delete from studentinfo where student_id = 1";

stmt = conn.prepareStatement(sql);

int count = stmt.executeUpdate(sql);

System.out.println("您删除了" + count + "条语句");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void update() {

DBUtil dbUtil = new DBUtil();

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

conn = dbUtil.getConnection();

String sql = "update studentinfo set student_name='sakura' where student_id = 5";

stmt = conn.prepareStatement(sql);

int count = stmt.executeUpdate(sql);

System.out.println("您修改了" + count + "条语句");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class DBUtil {

static Connection conn = null;

static Statement stmt = null;

static ResultSet rs= null;

static String url ="jdbc:mysql://localhost:3306/tyzx1?useUnicode=true&characterEncoding=UTF-8";

public Connection getConnection(){

try {

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

conn = DriverManager.getConnection(url,"root","2013005488");

System.out.println("success");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值