App下載
首頁javaconnectionJava JDBC - 如何使用JDBC將值插入到sqlserver數(shù)據(jù)庫中

Java JDBC - 如何使用JDBC將值插入到sqlserver數(shù)據(jù)庫中

我們想知道如何使用JDBC將值插入到sqlserver數(shù)據(jù)庫中。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    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";
    Class.forName(driver);
    con = DriverManager.getConnection(url, username, password);
    Statement st = con.createStatement();
    int val = st.executeUpdate("insert into dbo.Company " + "values('abc', 2)");
    System.out.println("1 row affected");
  }
}