Java数据库(ODBC)连接

 Java数据库(ODBC)连接 

简单的数据库连接示例:

 

  1. package com.test1;  
  2. /** 
  3.  * 演示使用jdbc-odbc桥连方式连接数据库 
  4.  * 1.配置数据源 
  5.  * 2.程序中去连接数据源 
  6.  * 3.程序中关闭数据源 
  7.  */  
  8. import java.sql.*;  
  9.   
  10. public class Test1 {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.         Connection ct=null;  
  18.         Statement sm=null;  
  19.         try{  
  20.             //1.加载驱动(作用是把需要的驱动程序加入内存)  
  21.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  22.             //2.得到连接,指定连结到那个数据源,用户名,和密码  
  23.             ct = DriverManager.getConnection("jdbc:odbc:mytest","gkp","123");  
  24.             //3.创建Statement或者是PreparedStatement[区别]  
  25.             //Statement用处是:主要是用于发送sql语句,到数据库  
  26.             sm= ct.createStatement();  
  27.             //4.执行(crud,创建数据库,备份数据库,删除数据...)  
  28.             //1.演示添加一条数据到表中  
  29.             //executeUpdate可以执行cud操作(添加,删除,修改)  
  30.             int i = sm.executeUpdate("insert into Emp values('1','软件工程')");  
  31.             if(i == 1){  
  32.                 System.out.println("添加OK");  
  33.             }else{  
  34.                 System.out.println("添加失败");  
  35.             }  
  36.               
  37.         }catch(Exception e){  
  38.             e.printStackTrace();  
  39.         }finally{  
  40.             try {  
  41.                 //关闭资源  
  42.                 if(sm!=null){  
  43.                 sm.close();  
  44.                 }  
  45.                 if(ct!=null){  
  46.                 ct.close();  
  47.                 }  
  48.             } catch (Exception e2) {  
  49.                 // TODO: handle exception  
  50.             }  
  51.         }  
  52.   
  53.     }  
  54.   
  55. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值