听说为了安全,在jsp与数据库连接的时候,禁止把代码写在网页中,而是调用生成的.class文件,可是该如何写数据库连接语句呢?
又该怎么连接呢???
又该怎么连接呢???
解决方案 »
- 在jsp页面里,点击按钮的时候,怎么把页面上的值保存到数据库中?
- 急求,jsp+servlet 上传图片 源码,要有注解,不然我怎么看?
- 提示需要安装插件
- js接收json数据问题
- sharedObject无法在服务器端生成.fso文件
- Jsp页面如何引用
- 一个网页究竟理论能同时容纳多少人
- 装好tomcat,但是jsp网页打不开啊~~
- 问个SQL漏洞的语句,如何测验
- 配置jsp运行环境的时候不小心把path里原来的内容更改确定了,怎么才能查到原来里面的内容~~
- 为什么我用FileOutputStream后close了,还不能renameto
- 请教MissingResourceException问题如何解决?
可以写一个数据库连接的类,在JSP文件中直接调用。
比如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();
} 就是这样的.然后你把需要的参数放进去进行连接就好
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();
}
}
}
调用时创建其对象,对象.方法名()
,另外请修改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
JSP+Servlet+JAVABEAN