【问题标题】:insert values into SQL database using JDBC使用 JDBC 将值插入 SQL 数据库
【发布时间】:2011-11-04 04:12:16
【问题描述】:

您好,我正在尝试使用 jdbc 将值插入 SQL 数据库。当前代码能够连接和查询 sql DB。我正在使用 executeupdate 插入值。这段代码没有给出任何错误。但是值没有被插入到 SQL DB 中。我怎样才能将值插入数据库???

  import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
  import com.microsoft.sqlserver.jdbc.SQLServerException;
  import java.sql.*;

    import java.sql.Connection;
class New{
Connection connection = null;
void condb() throws SQLServerException, SQLException{

  SQLServerDataSource dataSource = new SQLServerDataSource();
    dataSource.setServerName("OEl");
    dataSource.setPortNumber(1433);
    dataSource.setDatabaseName("Example");
    dataSource.setUser("sa");
    dataSource.setPassword("******");
        connection = dataSource.getConnection();
        connection.setAutoCommit(false);
        System.out.println("Connected to server !!!");
}
 void insertvalues() throws SQLException{
   Statement statement1=connection.createStatement();
                       statement1.executeUpdate("insert into dbo.Company " +
             "values('abc', 2)");

}
public static void main(String args[]) throws SQLServerException, SQLException{
New con=new New();
con.condb();
 con.insertvalues();
}
   }

【问题讨论】:

  • 应用程序几乎不应该使用 sa 作为用户。即使在示例中也不要这样做(这充其量只会分散注意力)。

标签: java sql jdbc


【解决方案1】:

您已将 autocommit 设置为 false 并且尚未提交。你认为会发生什么?来自Oracle's own docs on the subject

禁用自动提交模式后,在您显式调用commit方法之前,不会提交任何SQL语句。

要么:

connection.setAutoCommit (true);

当您创建连接时(尽管我们可能会将其作为一个选项打折扣,因为您将其明确设置为 false,这意味着您希望在事务中批量更改),或者:

connection.commit();

在从insertValues()返回之前。

您可能还想检查来自executeUpdate 的返回值,即使只是为了调试目的。对于插入,它为您提供插入的行数(在这种情况下应该是一)。

【讨论】:

    【解决方案2】:

    试试这个例子:

    这是一个工作..

    import java.sql.*;
    
    public class InsertValues{
        public static void main(String[] args) {
            System.out.println("Inserting values in  database table!");
            Connection con = null;
            String url = "jdbc:jtds:sqlserver://127.0.0.1:1433/Example";
            String username = "sa";
            String password = "";
            String driver = "net.sourceforge.jtds.jdbc.Driver";
            try{
                Class.forName(driver);
                con = DriverManager.getConnection(url,username,password);
                try{
                    Statement st = con.createStatement();
                    int val = st.executeUpdate("insert into dbo.Company " +"values('abc', 2)");
                    System.out.println("1 row affected");
                }
                catch (SQLException s){
                    System.out.println("SQL statement is not executed!");
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    注意:您必须在类路径中添加 sql 驱动程序(Dricer jar 文件)。 如果您使用的是任何 IDE,请在您的 Bulid 路径中。

    【讨论】:

    • 这对于 OPs 代码并没有真正的帮助,因为您不会摆弄自动提交等,尽管从工作代码开始通常是好的并逐渐改变它以匹配你自己的,直到它坏掉。我特别喜欢System.out.println("1 row affected"); 位,显然是基于狂野的乐观:-)
    【解决方案3】:

    插入后尝试关闭连接,可能是数据保存在缓冲区中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-21
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多