Insert data into a table on a database
import java.sql.*;import java.util.*;
import java.util.Date;
/*@Linda:08.08.2001*/
public class DBInsert {    private Connection       con;
    private DatabaseMetaData dbMeta;
    private String           driver =
        "com.microsoft.jdbc.sqlserver.SQLServerDriver";    public DBInsert() {
        createConnection();
        executeSQL();
    }    private void createConnection() {        Properties props = new Properties();        props.put("user", "sa");
        props.put("password", "plato17");        try {
            Class.forName(driver);            con = DriverManager.getConnection(
                "jdbc:microsoft:sqlserver:"
                + "//linda-a7agpelgn:1051;databasename=sunrise", props);
        } catch (java.lang.ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }        printDate();
        printDBInfo();
        printLevel();
    }    /* Timestamp */
    private void printDate() {        Date      date = new Date();
        Timestamp time = new Timestamp(date.getTime());        System.out.println("Timestamp: " + time);
    }    private void executeSQL() {        String[] insert = { "Joseph", "Weinstein", "clj.databases", "B.E.A" };        try {
            String            sql  =
                "INSERT INTO NGMembers VALUES(?,?,?,?,?)";
            PreparedStatement stmt = con.prepareStatement(sql);            stmt.setInt(1, 19);            for (int i = 0; i < insert.length; i++) {
                stmt.setString(i + 2, insert[i]);
            }            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Throwable t) {    // ignore
            }
        }
    }    private void printDBInfo() {        try {
            dbMeta = con.getMetaData();            System.out.println("DB-Name: " + dbMeta.getDatabaseProductName());
            System.out.println("DB-Version: "
                               + dbMeta.getDatabaseProductVersion());
            System.out.println("Driver-Name: " + dbMeta.getDriverName());
            System.out.println("Driver-Version: "
                               + dbMeta.getDriverVersion());
            System.out.println("===============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }    /* Supported ANSI level */
    private void printLevel() {        try {
            dbMeta = con.getMetaData();            if (dbMeta.supportsANSI92FullSQL()) {
                System.out.println("ANSI92 full SQL grammar is supported.");
            } else if (dbMeta.supportsANSI92IntermediateSQL()) {
                System.out
                    .println("ANSI92 intermediate SQL grammar is supported.");
            } else if (dbMeta.supportsANSI92EntryLevelSQL()) {
                System.out
                    .println("ANSI92 Entry Level SQL grammar is supported.");
            } else {
                System.out.println("ANSI92 SQL grammar is not supported.");
            }
        } catch (SQLException ec) {
            ec.printStackTrace();
        }
    }    public static void main(String[] args) {
        new DBInsert();
    }
}

解决方案 »

  1.   

    注册事件侦听器,然后加入以下代码
    public void actionPerformed(ActionEvent e) {
    ResultSet rs=null;
    String username="";
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
    String user="sa";
    String password="";
    Connection conn= DriverManager.getConnection(url,user,password);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String sql="insert into class(username) values('qqq')";
    stmt.executeUpdate(sql);
      }