有人可以帮我解决这个问题吗?我正在尝试使用 JAVA 执行 SQL 查询(到 Microsoft SQL)。但是,我的 table 上什么也没发生。而且,也不异常(exception)。但我很确定这段代码直接连接到我的数据库。这是我的代码。
package Testing;
//import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class TestingClass {
public static void main(String[] srg)
{
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // Start JDBC
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=OPERATIONS"; // Connect the server and the database
String userName="AppControlTeam";
String userPwd="*****";
//
Connection connection = null;
try{
Class.forName(driverName);
connection = DriverManager.getConnection(dbURL,userName,userPwd);
String sql = "DELETE FROM GFR_GROWTH_RATE";
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
connection.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
我已经在包中添加了 JDBC 驱动程序。另外,我非常确定这不是类路径问题,因为通过此代码连接到数据库已经成功。
预先感谢可以提供帮助的人! :D
-次郎
最佳答案
如果您未将 JDBC 驱动程序的自动提交属性设置为 true
,您的 DELETE
可能会回滚。也许尝试打电话
connection.commit();
// right before...
connection.close();
或者:
connection.setAutoCommit(true);
// before...
PreparedStatement statement = connection.prepareStatement(sql);
关于java - 使用 JAVA 查询 SQL - 没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10053876/