博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql的db.properties,经过properties配置文件连接数据库
阅读量:5011 次
发布时间:2019-06-12

本文共 5070 字,大约阅读时间需要 16 分钟。

当前位置:我的异常网» 数据库 » 经过properties配置文件连接数据库

经过properties配置文件连接数据库

www.myexceptions.net  网友分享于:2013-11-20  浏览:4次

通过properties配置文件连接数据库

**

* @作者:Jcuckoo

* @日期:2008-11-8

* @版本:V 1.0

*/

db.properties

DBDriver=sun.jdbc.odbc.JdbcOdbcDriver

Connection=jdbc:odbc:login

User=Jcuckoo

Password=

dbPool.java

import java.io.*;

import java.util.*;

import java.sql.*;

public class dbPool{

private static dbPool instance = null;

//取得连接

public static synchronized Connection getConnection() {

if (instance == null){

instance = new dbPool();

}

return instance._getConnection();

}

private dbPool(){

super();

}

private  Connection _getConnection(){

try{

String sDBDriver  = null;

String sConnection   = null;

String sUser = null;

String sPassword = null;

Properties p = new Properties();

InputStream is = getClass().getResourceAsStream("/db.properties");

p.load(is);

sDBDriver = p.getProperty("DBDriver",sDBDriver);

sConnection = p.getProperty("Connection",sConnection);

sUser = p.getProperty("User","");

sPassword = p.getProperty("Password","");

Properties pr = new Properties();

pr.put("user",sUser);

pr.put("password",sPassword);

pr.put("characterEncoding", "GB2312");

pr.put("useUnicode", "TRUE");

Class.forName(sDBDriver).newInstance();

return DriverManager.getConnection(sConnection,pr);

}

catch(Exception se){

System.out.println(se);

return null;

}

}

//释放资源

public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)

throws SQLException

{

rs.close();

ps.close();

conn.close();

}

}

StudentDAO.java

public class StudentDAO {

public StudentDAO() {

}

Connection conn;

Statement st;

ResultSet rs;

public boolean checkLogin(String userName,String userPwd){

String sql="select * from student where userName='"+userName+"' and userPwd='"+userPwd+"'";

try {

conn=dbPool.getConnection();

st=conn.createStatement();

rs=st.executeQuery(sql);

if(rs.next()){

conn.close();

return true;

}else{

conn.close();

return false;

}

} catch (SQLException e) {

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

e.printStackTrace();

return false;

}

}

public boolean checkLogin(StudentForm student){

String sql="select * from student where userName='"+student.getUserName()+"' and userPwd='"+student.getUserPwd()+"'";

try {

conn=dbPool.getConnection();

st=conn.createStatement();

rs=st.executeQuery(sql);

if(rs.next()){

conn.close();

return true;

}else{

conn.close();

return false;

}

} catch (SQLException e) {

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

e.printStackTrace();

return false;

}

}

public List getAllStudent(){

String sql="select * from student ";

List students=new ArrayList();

try {

conn=dbPool.getConnection();

st=conn.createStatement();

rs=st.executeQuery(sql);

while(rs.next()){

Student student=new Student(rs.getString("xuehao"),

rs.getString("userName"),

rs.getString("userPwd"),

rs.getInt("czxt"),

rs.getInt("wjyl"),

rs.getInt("sjjg")

);

students.add(student);

}

} catch (SQLException e) {

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

e.printStackTrace();

return null;

}

return students;

}

public Student getStudentByXuehao(String xuehao){

String sql="select * from student where xuehao='"+xuehao+"'";

Student student=null;

try {

conn=dbPool.getConnection();

st=conn.createStatement();

rs=st.executeQuery(sql);

if(rs.next()){

student=new Student(rs.getString("xuehao"),

rs.getString("userName"),

rs.getString("userPwd"),

rs.getInt("czxt"),

rs.getInt("wjyl"),

rs.getInt("sjjg")

);

}

return student;

} catch (SQLException e) {

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

e.printStackTrace();

return null;

}

}

public boolean deleteStudent(String xuehao){

String sql="delete from student where xuehao='"+xuehao+"'";

conn=dbPool.getConnection();

try {

st=conn.createStatement();

st.execute(sql);

conn.close();

return true;

} catch (SQLException e) {

e.printStackTrace();

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

return false;

}

}

public boolean updateStudent(Student student){

String sql="update student set userName='" +student.getUserName()+

"',userPwd='" +student.getUserPwd()+"',czxt="+student.getCzxt()+

",wjyl="+student.getWjyl()+",sjjg="+student.getSjjg()+" where xuehao='"+student.getXuehao()+"'";

conn=dbPool.getConnection();

try {

st=conn.createStatement();

st.execute(sql);

conn.close();

return true;

} catch (SQLException e) {

e.printStackTrace();

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

return false;

}

}

public boolean insertStudent(Student student){

String sql="insert into student values('"+student.getXuehao()+"','" +student.getUserName()+

"','" +student.getUserPwd()+"',"+student.getCzxt()+

","+student.getWjyl()+","+student.getSjjg()+")";

//System.out.println(sql);

conn=dbPool.getConnection();

try {

st=conn.createStatement();

st.execute(sql);

conn.close();

return true;

} catch (SQLException e) {

e.printStackTrace();

try {

conn.close();

} catch (SQLException e1) {

e1.printStackTrace();

}

return false;

}

}

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/12/01/3418933.aspx

文章评论

你可能感兴趣的文章
SGU438_The Glorious Karlutka River =)
查看>>
Linux集群及LVS简介
查看>>
简单几何(直线与圆的交点) ZOJ Collision 3728
查看>>
Codeforces Round #327 (Div. 2)
查看>>
如何解决Provisional headers are shown问题(转)
查看>>
开发网站遇到的bug
查看>>
实现简单的接口自动化测试平台
查看>>
EXCEL工作表合并
查看>>
Prime Path
查看>>
ODAC(V9.5.15) 学习笔记(三)TOraSession(2)
查看>>
单纯形法
查看>>
SQL中的replace函数
查看>>
java中的类型安全问题-Type safety: Unchecked cast from Object to ...
查看>>
如何解决最后一个尾注引用显示与致谢混为一谈的问题-下
查看>>
Java Socket编程 - 基于TCP方式的二进制文件传输【转】http://blog.csdn.net/jia20003/article/details/8248221...
查看>>
阅读之https及加密原理
查看>>
HDOJ4550 卡片游戏 随便销毁内存的代价就是wa//string类的一些用法
查看>>
css文本样式text、字体样式font
查看>>
洛谷 P1020 导弹拦截(LIS)
查看>>
python判断图片是否损坏
查看>>