当前位置:
文档之家› JAVA通过JDBC ODBC连接SQL SERVER 数据库代码
JAVA通过JDBC ODBC连接SQL SERVER 数据库代码
try { //1.加载驱动 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2.得到连接(锁定连接到哪个数据源) //如果配置数据源时选择windows nt验证,则不需要“用户名”,“密码” ("sa","sa")
ct=DriverManager.getConnection("jdbc:odbc:mytest","sa","sa");
} }
} }
//3.创建Statement或者PrepareStatement(区别) //Statement主要用于发送SQL语句会返回一个int如果int=n则修改了n条记 录
sm=ct.createStatement();
//4.执行(CRUD)增删改查 //executeUpdate可以实现添加删除修改 int i=sm.executeUpdate("insert into emp values('一切有为法, 如梦幻泡影')");
//关闭资源!!!(这个一定要记得哦) //关闭顺序是,谁后创建则先关闭
try { //为了程序健壮 if (sm!=null) {sm.close();} if(ct!=null) {ct .close();}
} catch (Exception e) { e.printStackTrace();
//查看是否修改成功 if(i==0) { System.out.println("修改出错"); } else { System.out.println(i+"条记录已修改"); }
} catch (Exception e) { //显示异常信息 e.printStackTrace(); } finally{
/* * 1.配置数据源 * 2.在程序中连接数据源 */
package jdbc_odbc; import java.sql.*; public class jdbc_odbc_SQL2000 {
public static void main(String[] args) {
Connection ct=null; Statement sm=null;