听说为了安全,在jsp与数据库连接的时候,禁止把代码写在网页中,而是调用生成的.class文件,可是该如何写数据库连接语句呢?
又该怎么连接呢???

解决方案 »

  1.   

    不是禁止,而是本来就可以写在网页中,只是那样效率低下,可重用性低。
    可以写一个数据库连接的类,在JSP文件中直接调用。
      

  2.   

    这种问题为什么天天有人问?db连接。都是写成通用组件,后台的事情,你为什么就喜欢写到*.jsp里面去呢。!?
      

  3.   

    你直接在项目的src写类  然后在jsp里 写上类名 补全下 jsp会自己导入这个类
      

  4.   

    楼主你听错了吧.一般是写在.ini文件或者是.properties文件中.很简单
    比如jdbc
    DriverClass=mysql.jdbc.Driver
    url=mysql://localhost:3306......
    username=root
    password=123456
    然后保存为.properties文件就可以
    然后在JAVA文件中读取
     Properties prop = new Properties(); 
            InputStream in = Object.class.getResourceAsStream("/jdbc.properties"); 
            try { 
                prop.load(in); 
                param1 = prop.getProperty("username").trim(); 
                param2 = prop.getProperty("password").trim(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 就是这样的.然后你把需要的参数放进去进行连接就好
      

  5.   

    如果是JDBC连接数据库类,一般这样写:
    package database;import java.sql.*;public class DataBaseConn
    {
    // PreparedStatement pstmt=null;
    public Connection conn=null; 
    public Statement stmt = null;
    public ResultSet rs=null;
     String dbDriver="net.sourceforge.jtds.jdbc.Driver";
     String url="jdbc:jtds:sqlserver://localhost:1433;databasename=CunshewangWebsite";
     String username="sa";
     String password="q1w2e3r4t5y6u7"; 


     // 实例化,装载JDBC驱动程序
     public DataBaseConn()
     {
     try
     {
        Class.forName(dbDriver);
      
     conn=DriverManager.getConnection(url,username,password);

     }
        catch(Exception e){
        e.printStackTrace();
        }
     }  
     //取得数据库连接
     public Connection getConnection()
    {
    return this.conn ;
    }
     
     
     // 返回查询后的数据集  public ResultSet executeQuery(String sql)
     {
        try
        {
        
        stmt=conn.createStatement();
       rs=stmt.executeQuery(sql);
        }
       catch(SQLException e){
       e.printStackTrace();
      
       }
      
       return rs;
     }
     
     // 对数据库的更新操作
     public int executeUpdate(String sql)
     {
     int num=0;
      try
        {
        
       stmt=conn.createStatement();
       num=stmt.executeUpdate(sql);
        }
       catch(SQLException e){
       e.printStackTrace();
       }
       return num;
     }
     
     
     // 关闭数据库
     public void closeDB()
     {
      try
      {
      if(conn!=null){
    // rs.close();
      conn.close();
     
      }
       
      }
        catch(SQLException e) {
        e.printStackTrace();
        }
     }

     
    }
    调用时创建其对象,对象.方法名()
      

  6.   

    在项目的包中导入相应的JDBC驱动包,然后把下面的代码放在一个放在class里面,一个放在jdbc.properties里面,注意jdbc.properties放在src目录下,然后直接调用getConnection()获取连接
    ,另外请修改jdbc.properties中的设置。
    package ibatis.test.model;import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;/**
     * @author king
     * jdbc连接工厂
     *
     */
    public class JdbcFactory {    private Connection conn = null;
        private static final String FILEPATH="src/jdbc.properties";    private Connection connectionFactory(String filePath) throws Exception{
            Map<String, Object> params=new HashMap<String, Object>();
            try {
                params = readProperties(filePath);
            } catch (Exception e) {
                throw new Exception("文件不存在,请检查文件目录");
            }
            int type = checkProperties(filePath);
            switch (type) {
            case 1:return getJdbcConnection(params.get("driver_class"),params.get("url"),params.get("username"),params.get("password"));
            case 2:return getDatasourceConnection(params.get("datasource"));
            case 3:return null;
            default: throw new Exception("错误的连接方式");
            }    }    public static Connection getConnection()throws Exception{
            return new JdbcFactory().connectionFactory(FILEPATH);
        }    /**
         * 以Datasource方式获取连接
         * @param datasource
         * @return
         */
        private Connection getDatasourceConnection(Object datasource) {
            //TODO Datasource方式连接
            return null;
        }    /**
         * 以JDBC方式获取连接
         * @param driver_class
         * @param url
         * @param username
         * @param password
         * @return
         * @throws Exception
         */
        private Connection getJdbcConnection(Object driver_class, Object url,Object username, Object password)throws Exception {
            String driver = (String) driver_class;
            String jdbc_url = (String) url;
            String jdbc_user = (String) username;
            String jdbc_pass = (String) password;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_pass);
                return conn;
            } catch (ClassNotFoundException e) {
                throw e;
            } catch (SQLException e) {
                throw e;
            }
        }    /**
         * 验证配置文件设置
         * dbms;DBMS类型
         * connecttype连接方式  可指定为:jdbc或者datasource以及provider方式
         * 当指定了参数connecttype为jdbc时,以下参数必需提供
         *    driver_class;
         *    url;
         *    username;
         *    password;
         * 当指定了参数connecttype为datasource时,以下参数必需提供
         *    datasource;
         * @return
         * 1 jdbc
         * 2 datasource
         * 3 provider
         * @throws Exception
         */
        private int checkProperties(String filePath)throws Exception{
            Map<String, Object> params=new HashMap<String, Object>();
            int type=0;
            try {
                try {
                    params = readProperties(filePath);
                } catch (Exception e) {
                    throw new Exception("文件不存在,请检查文件目录");
                }
                if(params!=null){
                    if(params.containsKey("dbms")){
                        if(params.get("dbms").equals("")){
                            throw new Exception("DBMS类型:dbms不能为空");
                        }
                    }
                    if(params.containsKey("connecttype")){
                        if(params.get("connecttype").equals("")){
                            throw new Exception("连接方式:connecttype不能为空");
                        }else{
                            if(params.get("connecttype").equals("jdbc")){
                                type = 1;
                                if(params.containsKey("driver_class")){
                                    if(params.get("driver_class").equals("")){
                                        throw new Exception(params.get("connecttype")+"连接方式下driver_class不能为空");
                                    }
                                }
                                if(params.containsKey("url")){
                                    if(params.get("url").equals("")){
                                        throw new Exception(params.get("connecttype")+"连接方式下url不能为空");
                                    }
                                }
                                if(params.containsKey("username")){
                                    if(params.get("username").equals("")){
                                        throw new Exception(params.get("connecttype")+"连接方式下username不能为空");
                                    }
                                }
                                if(params.containsKey("password")){
                                    if(params.get("password").equals("")){
                                        throw new Exception(params.get("connecttype")+"连接方式下password不能为空");
                                    }
                                }
                            }else if(params.get("connecttype").equals("datasource")){
                                type = 2;
                                if(params.containsKey("datasource")){
                                    if(params.get("datasource").equals("")){
                                        throw new Exception(params.get("connecttype")+"连接方式下datasource不能为空");
                                    }
                                }
                            }else if(params.get("connecttype").equals("provider")){
                                type = 3;
                                throw new Exception("暂时未添加此连接方式");                        }else{
                                throw new Exception("连接方式:connecttype可指定为:jdbc或者datasource以及provider方式");
                            }
                        }
                    }
                }
                return type;
            } catch (Exception e) {
                throw e;
            }
        }    /**
         * 根据文件路径和key读取对应的value
         * @param key
         * @param filePath 文件路径
         * @return 可对应value
         * @throws Exception 文件不存在或者错误的key
         */
        @SuppressWarnings("unused")
        private String readValue(String key,String filePath ) throws Exception{
            Properties props = new Properties();
            String value="";
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));
                props.load(in);
                value = props.getProperty(key);
                return value;
            } catch (Exception e) {
                throw new Exception("文件不存在或错误的key,请检查文件路径和key是否正确");
            }    }    /**
         * 读取properties的全部信息
         * @param filePath
         */
        @SuppressWarnings("unchecked")
        private Map<String, Object> readProperties(String filePath)throws Exception {
            Map<String, Object> params=new HashMap<String, Object>();
            Properties props = new Properties();
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));
                props.load(in);
                Enumeration en = props.propertyNames();
                while (en.hasMoreElements()) {
                    String key = (String) en.nextElement();
                    String Property = props.getProperty(key)==null?"":props.getProperty(key);
                    params.put(key, Property);
                }
                return params;
            } catch (Exception e) {
                throw new Exception("文件不存在,请检查文件路径");
            }
        }}#DBMS类型:sqlserver,mysql,oracle
    dbms=sqlserver#connecttype 连接方式,可指定为:jdbc或者datasource以及provider方式
    connecttype=jdbc#当指定了参数connecttype为jdbc时,以下参数必需提供
    driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriver
    url=jdbc:sqlserver://localhost:1039;DatabaseName=kingtbls;SelectMethod=cursor
    username=king
    password=king#当指定了参数connecttype
      

  7.   

    分层,jsp只是用来展示的。不要写java代码
      

  8.   

    使用MVC....
    JSP+Servlet+JAVABEAN