//首先导入sql server2008的jar包:sqljdbc4.jar,注意在一般的电脑上即使做了这样的操作仍然是吧不能连接上SQL Server 2008的,如果想连上SQL SQLSERVER2008还要做一下配置:请看博文:http://hi.baidu.com/1032610746/item/a1a84d086a9225e43599023f 和 http://hi.baidu.com/1032610746/item/41091f51549dfdd3d48bac36,如果想了解更多关于SQL SERVER应用方面的用法:请看:http://hi.baidu.com/1032610746/archive/tag/sql%20server
package cn.edu.hactcm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author <a href="mailto:1032610746@qq.com">涂作泉</a>
*
* @version 1.2 2012-5-20
* @sqlserverjdbc
*/
public class InsertDemo {
public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//定义SQL Server数据库的连接地址
public static final String DBURL = "jdbc:sqlserver://localhost:1433;databaseName=mobileshop;";
public static final String DBUSER = "sa";
public static final String DBPASS = "123456";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null; //数据库连接
Statement stmt = null; //数据库操作
String sql = "insert into mobileshop_brand(id,brandname) values(1,'诺基亚')";
Class.forName(DBDRIVER); //加载数据库驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
stmt = conn.createStatement(); //实例化statement对象
stmt.executeUpdate(sql);//执行数据库操作
stmt.close();
stmt.close();
conn.close();
}
}