Java JDBC How to - Insert values into sqlserver database using JDBC


We would like to know how to insert values into sqlserver database using JDBC.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
// w  w w. j  ava2s  .co m
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://";
    String username = "sa";
    String password = "";
    String driver = "net.sourceforge.jtds.jdbc.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");