TestDatabaseUpdate.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.ResultSet;
  5. public class TestDatabaseUpdate {
  6. public static void main(String[] args) {
  7. try {
  8. // 加载驱动
  9. Class.forName("dm.jdbc.driver.DmDriver");
  10. // 连接数据库
  11. String url = "jdbc:dm://192.168.0.145:30236?charSet=utf8";
  12. String username = "WATERSHED";
  13. String password = "WaterShed./1224";
  14. Connection conn = DriverManager.getConnection(url, username, password);
  15. Statement stmt = conn.createStatement();
  16. // 测试更新模型ID为111的记录
  17. Long modelId = 111L;
  18. // 先查询当前值
  19. String selectSql = "SELECT rotation_x, rotation_y, rotation_z, scale_x, scale_y, scale_z FROM WATERSHED_MODEL WHERE model_id = " + modelId;
  20. ResultSet rs = stmt.executeQuery(selectSql);
  21. if (rs.next()) {
  22. System.out.println("=== 更新前 ===");
  23. System.out.println("rotation_x: " + rs.getDouble("rotation_x"));
  24. System.out.println("rotation_y: " + rs.getDouble("rotation_y"));
  25. System.out.println("rotation_z: " + rs.getDouble("rotation_z"));
  26. System.out.println("scale_x: " + rs.getDouble("scale_x"));
  27. System.out.println("scale_y: " + rs.getDouble("scale_y"));
  28. System.out.println("scale_z: " + rs.getDouble("scale_z"));
  29. }
  30. // 执行更新
  31. String updateSql = "UPDATE WATERSHED_MODEL SET " +
  32. "rotation_x = 0, rotation_y = 0, rotation_z = 260, " +
  33. "scale_x = 1, scale_y = 1, scale_z = 1 " +
  34. "WHERE model_id = " + modelId;
  35. int rows = stmt.executeUpdate(updateSql);
  36. System.out.println("\n更新了 " + rows + " 行");
  37. // 再次查询验证
  38. rs = stmt.executeQuery(selectSql);
  39. if (rs.next()) {
  40. System.out.println("\n=== 更新后 ===");
  41. System.out.println("rotation_x: " + rs.getDouble("rotation_x"));
  42. System.out.println("rotation_y: " + rs.getDouble("rotation_y"));
  43. System.out.println("rotation_z: " + rs.getDouble("rotation_z"));
  44. System.out.println("scale_x: " + rs.getDouble("scale_x"));
  45. System.out.println("scale_y: " + rs.getDouble("scale_y"));
  46. System.out.println("scale_z: " + rs.getDouble("scale_z"));
  47. }
  48. // 关闭连接
  49. rs.close();
  50. stmt.close();
  51. conn.close();
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }