【发布时间】: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 作为用户。即使在示例中也不要这样做(这充其量只会分散注意力)。