数据库连接JDBC

  1. 温馨提示:在使用这程序之前,需要向项目中导入相应数据库的驱动并创建数据库

  2. 第一种连接MySQL

  3. importjava.sql.Connection;
  4. importjava.sql.DriverManager;
  5.  
  6. /**
  7.  * 连接 MySQL数据库
  8. * 下载驱动网址:”http://www.mysql.org”
  9.  */
  10. public classDemo2_GetConn {
  11.    public Connection conn = null; // 创建Connection对象
  12.  
  13.    public ConnectiongetConnection() { // 获取数据库连接方法
  14.  
  15.       try {
  16.          //Class.forName(com.mysql.jdbc.Driver); // 加载数据库驱动
  17.          DriverManager.registerDriver(newcom.mysql.jdbc.Driver());
  18.          //指定连接数据的URL
  19.          String url = "jdbc:mysql://localhost:3306/day08";
  20.          String user = "root";     //指定连接数据库的用户名
  21.          String password = "root"; //指定连接数据库的密码
  22.          conn =DriverManager.getConnection(url, user, password);
  23.          if (conn != null) { // 如果Connection实例不为空
  24.             System.out.println("数据库连接成功");  //提示信息
  25.          }
  26.       } catch(Exception e) {
  27.          e.printStackTrace();
  28.       }//异常处理

  1.       return conn;//返回Connection
  2.    }
  3.  
  4.    /**
  5.     * @param args
  6.     */
  7.    public static voidmain(String[] args) {//主方法
  8.       Demo2_GetConn getConn = newDemo2_GetConn(); // 创建Demo2_GetConn对象
  9.       getConn.getConnection(); // 调用连接数据库方法
  10.  
  11.    }
  12.  
  13. }
  14.  
  15.  
  16.  
  17. 第二种连接SQL Server
  18.  
  19. importjava.sql.Connection;
  20. importjava.sql.DriverManager;
  21.  
  22. /**
  23.  * 连接SQL Server2005数据库连接SQL
  24.  * Server2005数据库应用的驱动程序为"sqljdbc.jar",可以到Microsoft的官方网站上下载地址:
  25.  * "http://www.microsoft.com"
  26.  *
  27.  */
  28. public classDemo3_Server {
  29.    public Connection conn = null; // 创建Connection对象
  30.  
  31.    // 获取数据库连接方法
  32.    public ConnectiongetConnection() {
  33.       // 加载数据库驱动
  34.       try {
  35.        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive");
  36.          // 指定连接数据库的URL
  37.          String url = "jdbc:sqlserver://localhost:1433;databaseName=db_database19";
  38.          String user = "sa"; // 指定连接数据库的用户
  39.          String passWord = ""; // 指定连接数据库的密码
  40.          conn =DriverManager.getConnection(url, user, passWord);
  41.          // 如果Connection实例不为空
  42.          if (conn != null) {
  43.             System.out.println("数据库连接成功"); // 提示信息
  44.          }
  45.       } catch(Exception e) {
  46.  
  47.          e.printStackTrace();
  48.       }
  49.       return conn; // r返回Connection对象
  50.    }
  51.  
  52.    /**
  53.     * @param args
  54.     */
  55.    public static voidmain(String[] args) {
  56.       Demo3_Server getConn = newDemo3_Server();// 创建getConn对象
  57.       getConn.getConnection(); // 调用连接数据库方法
  58.  
  59.    }
  60.  
  61. }
  62.  
  63.  
  64.  //第三连接

  65. importjava.sql.Connection;
  66. importjava.sql.DriverManager;
  67.  
  68. /**
  69.  *  连接Oracle11g数据库应用的程序这"ojdbc6.jar",可以在Oracle
  70.  * 装后的文件夹中找到,路径是"\product\11.2.0\dbhome_1\jdbc\lib"
  71.  *
  72.  */
  73. public classDemo3_Oracle {
  74.    public Connection conn = null;     //创建Connection
  75.    public ConnectiongetConnection(){ //获取连接方法
  76.       try {
  77.          Class.forName("oracle.jdbc.driver.OracleDriver");
  78.          String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //指定连接数据库的URL
  79.          String user = "system"; //数据库用户名
  80.          String passWord = "justice";//指定连接数据库的密码
  81.          conn =DriverManager.getConnection(url, user, passWord);
  82.          if(conn != null){
  83.             System.out.println("连接成功");
  84.          }
  85.         
  86.       } catch(Exception e) {
  87.         
  88.          e.printStackTrace();
  89.       }     //加载数据库驱动
  90.       return conn;      //返回Connection对象
  91.    }
  92.   
  93.    /**
  94.     * @param args
  95.     */
  96.    public static voidmain(String[] args) {
  97.       Demo3_Oracle getConn = newDemo3_Oracle();
  98.       getConn.getConnection();  //调用连接数据库方法
  99.    }
  100.  
  101. }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值