简体   繁体   English

使用JDBC将值插入SQL数据库

[英]insert values into SQL database using JDBC

hi am trying to insert values into SQL database using jdbc. 您好,我尝试使用jdbc将值插入SQL数据库。 Current code able to connect and query sql DB. 当前代码能够连接和查询sql DB。 am using executeupdate to insert the values .this code is not giving any errors.but values are not getting inserted into SQL DB. 我正在使用executeupdate插入值。此代码未出现任何错误。但是值未插入SQL DB。 how can i insert values into 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();
}
   }

You've set autocommit to false and haven't committed. 您已将autocommit设置为false并且尚未提交。 What did you think would happen? 认为会发生什么? From Oracle's own docs on the subject : 来自Oracle自己的文档

After the auto-commit mode is disabled, no SQL statements are committed until you call the method commit explicitly. 禁用自动提交模式后,在您显式调用方法commit之前,不会提交任何SQL语句

Either: 或者:

connection.setAutoCommit (true);

when you create the connection (although we can probably discount that as an option since you explicitly set it to false, meaning you wanted to batch up your changes in a transaction), or: 在创建连接时(尽管我们可以将其作为选项取消,因为您已将其显式设置为false,这意味着您希望批量处理交易中的更改),或:

connection.commit();

before returning from insertValues() . insertValues()返回之前。

You may also want to check the return value from executeUpdate even if only for debugging purposes. 您可能还想检查executeUpdate的返回值,即使仅出于调试目的。 For an insert, it gives you the number of rows inserted (should be one, in this case). 对于插入,它为您提供了插入的行数(在这种情况下,应为1)。

just try with this example : 只需尝试以下示例:

this is working one.. 这是工作之一。

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();
        }
    }
}

NB: you must added the sql driver (Dricer jar file) in your class path. 注意:您必须在类路径中添加sql驱动程序(Dricer jar文件)。 if you are using any IDE, in your Bulid path. 如果您使用任何IDE,请在Bulid路径中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM