开始准备工作,需要先把jdbc包引入。如下图。
有了上面划红线的那个包之后,下面是实现增删改查的全部代码,有注释。
差点忘了,还需要准备好数据库表,如下图,一个库:test,一个表user,三个字段,name、age、sex
package test; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class TestJdbc { private static Connection getConn(){//链接数据库 Connection conn = null; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"; String username = "root"; String password = "root"; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (Exception e) { //可以什么都不写 throw new RuntimeException(e); } return conn; } private static int insert(String name , String age, String sex){//新增数据 int a = 0; Connection conn = getConn(); String sql = "insert into user (name, age , sex) value (?, ?, ?)"; PreparedStatement pstmt; try{ pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2, age); pstmt.setString(3, sex); a = pstmt.executeUpdate(); System.out.println("新增数量: " + a); pstmt.close(); conn.close(); }catch(Exception e){ //可以什么都不写 } return a; } private static int update(String age, String name) {//更新数据 Connection conn = getConn(); int i = 0; String sql = "update user set age= ? where name= ? "; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, age); pstmt.setString(2, name); i = pstmt.executeUpdate(); System.out.println("更新数据量: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } private static Integer getAll() {//查询数据 Connection conn = getConn(); String sql = "select * from user"; PreparedStatement pstmt; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("==========================================="); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + "\t"); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print("\t"); } } System.out.println(""); } System.out.println("========================================="); } catch (SQLException e) { e.printStackTrace(); } return null; } private static int delete(String name) {//删除数据 Connection conn = getConn(); int i = 0; String sql = "delete from user where name= ? "; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, name); i = pstmt.executeUpdate(); System.out.println("删除数量: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; } public static void main(String[] args){ insert("万磁王", "1500", "白袍巫师"); getAll(); update("800", "万磁王"); getAll(); delete("万磁王"); getAll(); } }
运行之后输出结果:
新增数量: 1 =========================================== tom 88 man 孙悟空 99999 齐天大圣 万磁王 1500 白袍巫师 ========================================= 更新数据量: 1 =========================================== tom 88 man 孙悟空 99999 齐天大圣 万磁王 800 白袍巫师 ========================================= 删除数量: 1 =========================================== tom 88 man 孙悟空 99999 齐天大圣 =========================================
初学的小朋友们有看不明白的地方就在下面留言吧