隨筆-124  評論-49  文章-56  trackbacks-0
          sql
          package org.epet.dao.impl;

          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;

          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          import com.sun.java_cup.internal.internal_error;

          public abstract class BaseDAO {
              
          private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
              
          private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1433;DatabaseName=epet";
              
          private static final String DATABASE_USER = "sa";
              
          private static final String DATABASE_PASSWORD = "accp";
              
              
          /**
               * 返回連接
               * 
          @return
               
          */

              
          public static Connection getConnection() {
                  Connection connection
          =null;
                  
          try {
                      Class.forName(DRIVER_CLASS);
                      connection 
          = DriverManager.getConnection(DATABASE_URL,
                              DATABASE_USER, DATABASE_PASSWORD);
          //                Context tx=new InitialContext();
          //                DataSource ds=(DataSource)tx.lookup("java:comp/env/food");
          //                connection=ds.getConnection();

                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }
           catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

                  
          return connection;
              }

              
          /**
               * 查詢
               * 
          @param sql
               * 
          @return
               
          */

              
          public static ResultSet getDate(String sql){
                  Connection connection
          =getConnection();
                  ResultSet resultSet
          =null;
                  
          try {
                      PreparedStatement preparedStatement
          =connection.prepareStatement(sql);
                      resultSet
          =preparedStatement.executeQuery();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }

                  
          return resultSet;
              }

              
              
          public static int dele(String sql,int id){
                  
          int result=0;
                  Connection connection
          =getConnection();
                  
          try {
                      PreparedStatement preparedStatement
          =connection.prepareStatement(sql);
                      preparedStatement.setInt(
          1, id);
                      result
          =preparedStatement.executeUpdate();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }

                  
          return result;
              }

          }

          mysql:
          /*show databases;
          create database aa;
          use aa;
          show tables;
          select * from userinfo limit 1,2;
          -----------------------------------------
          */

          public connection getConnection () throws SQLException{
            Class.forName(
          "com.mysql.jdbc.Driver");
            String url
          ="jdbc:mysql://127.0.1:3306/somken(數據庫名)";
            
          return DriverManager.getConnection(url,"root","root");
          }
          posted on 2009-11-30 08:21 junly 閱讀(528) 評論(0)  編輯  收藏 所屬分類: oracle/mysql/sql
          主站蜘蛛池模板: 永宁县| 大英县| 昭通市| 吴堡县| 白河县| 台湾省| 陕西省| 平阳县| 昭通市| 固安县| 铁力市| 南丰县| 宁乡县| 潮安县| 四川省| 甘洛县| 延川县| 石阡县| 隆安县| 灯塔市| 游戏| 剑阁县| 敦化市| 滦平县| 宣威市| 公主岭市| 张掖市| 卢氏县| 无为县| 永登县| 旌德县| 全州县| 武功县| 兰坪| 当涂县| 云梦县| 惠水县| 太谷县| 康保县| 台中县| 元朗区|