一般认为连接数据库,会把密码写在程序中,这是不方便的,也是不安全的,现在我为大家介绍一种方法。就是把密码放在property.conf配置文件上面,这样一来即方便了管理,也更加安全了。(纪念一下我大二Web课程设计)
property.conf
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=data
username=sa
password=123456
java 连接数据库的具体组件模型代码:
package com.database.bean;
import javax.naming.*;
import javax.sql.*;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Title: 数据访问类
* @ author Yinxing
*
*/
public class DBConnection
{
private Connection con = null;
private Statement st = null;
private ResultSet rs = null;
// private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 驱动程序
// private static String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=data";// 数据库连接字符串(data为数据库名)
// private static String username = "sa"; // 用户名
// private static String password = "123456"; // 密码
public DBConnection()
{
}
private static Properties p;
static
{
try {
p = new Properties();
InputStream is = DBConnection.class.getResourceAsStream("property.conf");
p.load(is);
is.close();
} catch (IOException e)
{
// TODO: handle exception
e.printStackTrace();
}
}
public static String getProperty(String key)
{
return p.getProperty(key);
}
private static String driver = DBConnection.getProperty("driver"); // 驱动程序
private static String url = DBConnection.getProperty("url");// 数据库连接字符串(data为数据库名)
private static String username = DBConnection.getProperty("username"); // 用户名
private static String password = DBConnection.getProperty("password"); // 密码
//------------------------------------------------------------------------------------------------
// 使用连接池获取数据库连接
public void getPoolConnection()
{
try
{
DataSource ds = null;
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mySQLServer2008");
con = ds.getConnection();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("获取数据库连接池对象出错!");
}
}
// 使用JDBC获取数据库连接
public void getConnection()
{
try
{
Class.forName(driver).newInstance();// 加载驱动程序
con = DriverManager.getConnection(url, username, password);// 连接数据库
// con.setAutoCommit(true);// 设置事务处理方式为自动提交
}
catch (Exception e)
{
System.out.println("连接服务器数据库出错!" + e.getMessage());
e.printStackTrace();
}
}
// 查询数据
public ResultSet executeQuery(String sql)
{
if (con == null)
{
getConnection();
// getPoolConnection();
}
try
{
st = con.createStatement();
rs = st.executeQuery(sql);
}
catch (Exception e)
{
System.out.println("查询数据时出错!" + e.getMessage());
e.printStackTrace();
}
return rs;
}
// 操作数据(数据的增加、修改和删除)
public boolean executeUpdate(String sql)
{
if (con == null)
{
getConnection();
// getPoolConnection();
}
try
{
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
return true;
}
catch (Exception e)
{
System.out.println("操作数据时出错!");
e.printStackTrace();
return false;
}
}
// 获取记录数
public int executeCount(String sql)
{
int num = 0;
if (con == null)
{
getConnection();
// getPoolConnection();
}
try
{
st = con.createStatement();
rs = st.executeQuery(sql);
while (rs.next())
{
num = rs.getRow();
}
}
catch (Exception e)
{
System.out.println("获取数据时出错!");
e.printStackTrace();
}
return num;
}
// 关闭数据库连接
public void closeConnection()
{
try
{
if (rs != null)
{
rs.close();
rs = null;
}
if (st != null)
{
st.close();
st = null;
}
if (con != null)
{
con.close();
con = null;
}
}
catch (Exception e)
{
System.out.println("关闭数据库时出错!");
e.printStackTrace();
}
finally
{
con = null;
}
}
}