BaseDAO

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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值