隨筆-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 閱讀(536) 評論(0)  編輯  收藏 所屬分類: oracle/mysql/sql
          主站蜘蛛池模板: 上犹县| 喀喇沁旗| 宁阳县| 沁阳市| 视频| 庆城县| 涡阳县| 盘锦市| 溧阳市| 盱眙县| 永济市| 崇文区| 会昌县| 日喀则市| 阜南县| 栖霞市| 荣昌县| 巴马| 静海县| 格尔木市| 岱山县| 察雅县| 乐都县| 宁晋县| 抚远县| 随州市| 巴里| 黄山市| 洪泽县| 兴海县| 仪陇县| 化州市| 永城市| 新民市| 尚义县| 潼关县| 钦州市| 宜兰市| 新邵县| 安图县| 定边县|