随笔-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 阅读(515) 评论(0)  编辑  收藏 所属分类: oracle/mysql/sql

只有注册用户登录后才能发表评论。


网站导航: