Idea是一款流行的Java集成開發環境,它可以方便地在Java項目中操作MySQL數據庫,包括增刪改查等基本操作。
//連接數據庫 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC", "root", "password"); //增加數據 String sql = "INSERT INTO employee (id, name, age, salary) VALUES (?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 1); pstmt.setString(2, "張三"); pstmt.setInt(3, 25); pstmt.setDouble(4, 5000.00); pstmt.executeUpdate(); //查詢數據 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee"); while(rs.next()){ System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getInt(3) + "\t" + rs.getDouble(4)); } //修改數據 String sql2 = "update employee set salary=? where name=?"; PreparedStatement pstmt2 = conn.prepareStatement(sql2); pstmt2.setDouble(1, 6000.00); pstmt2.setString(2, "張三"); pstmt2.executeUpdate(); //刪除數據 String sql3 = "delete from employee where name=?"; PreparedStatement pstmt3 = conn.prepareStatement(sql3); pstmt3.setString(1, "張三"); pstmt3.executeUpdate(); //關閉連接 pstmt.close(); stmt.close(); conn.close();
以上代碼演示了在Java中使用Idea進行MySQL的操作,包括了增加數據、查詢數據、修改數據、刪除數據等基本操作。