博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jdbc方式mysql语句增删改查
阅读量:5973 次
发布时间:2019-06-19

本文共 3325 字,大约阅读时间需要 11 分钟。

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;			}}

  

转载于:https://www.cnblogs.com/sakuraYLZ/p/5784334.html

你可能感兴趣的文章
40个非常有创意的国外LOGO欣赏(下)
查看>>
android中程序的退出和关闭
查看>>
543
查看>>
thymeleaf标签
查看>>
linux 上做mysql的数据备份
查看>>
初来咋到,记个号。
查看>>
ASA防火墙17 验证防火墙连通性
查看>>
突破Linux内核模块校验机制(转)
查看>>
小结Servlet/JSP技术点
查看>>
JAVA经典算法40题(13)
查看>>
SpringBoot使用JDBC实现增删改查
查看>>
百度地图显示标记
查看>>
记一下好用的js(三)
查看>>
国内Maven私服仓库
查看>>
spring springmvc 头文件
查看>>
关于代码风格的一些见解
查看>>
find,vim的编程设置,SUID,SGID,SBIT,软.硬链接,重定向
查看>>
IPSEC ××× LAN-to-LAN
查看>>
第七章 获取时间日期格式和延时:date命令、sleep命令
查看>>
SVN 图标显示异常
查看>>