JAVA简单数据库访问(JDBC)
前面介绍了JAVA使用ODBC连接数据库的方法,这里我们再来介绍使用JDBC连接数据库的方法。依然是通过示例说明。
开发环境:eclipse + SQL Server 2005,所需的sqljdbc下载(下载后解压) + JDK 5.0以上
首先导入sqljdbc.jar到项目中
![]()
选择 Import... ,弹出对话框
![]()
选择 Archive file 后,单击 Next 按钮
![]()
这样就完成了驱动文件的导入操作了!
之后的操作类似ODBC访问数据库文章中的内容,我在这里说明一下属性配置文件的内容吧!
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost;DatabaseName=WorldCup2006
user=ayumi
password=ayumi
url=jdbc:sqlserver://数据库服务器;DatabaseName=数据库名称
user=你登录数据库的用户名
password=你登录数据库的用户密码
数据库访问类(DataAccess.java),这里的数据库访问类的内容基本上没有说明改变,可以继续使用之前文章介绍的,这里仍然给出该类的内容。
/*
* DataAccess.java 数据库访问类
*
* Created on 2008年06月15日
* Author ajayumi
* Description : 该类主要是用于做数据库操作,可以直接使用该类便可完成绝大部分的数据库访问操作
*/
package ConnectionDemo;
![]()
import java.sql.*;
import java.io.*;
import java.util.Properties;
![]()
public class DataAccess {
![]()
// *****************************************************
// 私有变量
/** 驱动连接串 */
private String driver = null;
![]()
/** jdbc:subprotocol:subname 形式的数据库 url */
private String url = null;
![]()
/** 数据库用户,连接是为该用户建立的 */
private String user = null;
![]()
/** 用户的密码 */
private String password = null;
![]()
/** 属性文件存放路径 */
private String PropertiesFilePath = null;
![]()
/** 数据库连接对象 */
private Connection Conn = null;
![]()
// *****************************************************
![]()
// *****************************************************
// 公共常量
public final String DefaultDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
![]()
// *****************************************************
![]()
public DataAccess() {
}
![]()
/**
* 创建一个带参数的构造函数
*
* @param _driver,驱动连接串
* @param _url,jdbc:subprotocol:subname形式的数据库url
* @param _user,数据库用户,连接是为该用户建立的
* @param _password,用户的密码
*/
public DataAccess(String _driver, String _url, String _user,
String _password) {
this.SetDriver(_driver);
this.SetUrl(_url);
this.SetUser(_user);
this.SetPassword(_password);
}
![]()
/**
* 创建一个带参数的构造函数
*
* @param _PropertiesFilePath,属性文件存放路径
*/
public DataAccess(String _PropertiesFilePath) {
this.SetPropertiesFilePath(_PropertiesFilePath);
}
![]()
// *****************************************************
// 属性设置
/**
* 获取该类中的 driver
*
* @return driver
*/
public String getDriver() {
return this.driver;
}
![]()
/**
* 设置类中的 driver 为 _driver
*
* @param _driver,新建_driver
*/
public void SetDriver(String _driver) {
if (_driver == "" && _driver == null) {
System.out.println("No Setting Driver!");
} else {
this.driver = _driver;
}
}
![]()
/**
* 获取该类中的数据库 URL 的连接
*
* @return url
*/
public String getUrl() {
return this.url;
}
![]()
/**
* 设置类中的 url 为 _url
*
* @param _url,新建_url
*/
public void SetUrl(String _url) {
if (_url == "" && _url == null) {
System.out.println("No Setting Url!");
} else {
this.url = _url;
}
}
![]()
/**
* 获取该类中的数据库用户,连接是为该用户建立的
*
* @return user
*/
public String getUser() {
return this.user;
}
![]()
/**
* 设置类中的 user 为 _user
*
* @param _user,新建_user
*/
public void SetUser(String _user) {
if (_user == "" && _user == null) {
System.out.println("No Setting User!");
} else {
this.user = _user;
}
}
![]()
/**
* 获取该类中的数据库用户的密码
*
* @return password
*/
public String getPassword() {
return this.password;
}
![]()
/**
* 设置类中的 password 为 _password
*
* @param _password,新建_password
*/
public void SetPassword(String _password) {
if (_password == "" && _password == null) {
System.out.println("No Setting Password!");
} else {
this.password = _password;
}
}
![]()
/**
* 获取该类中的属性文件存放路径
*
* @return PropertiesFilePath
*/
public String getPropertiesFilePath() {
return this.PropertiesFilePath;
}
![]()
/**
* 设置类中的 PropertiesFilePath 为 _PropertiesFilePath
*
* @param _PropertiesFilePath,新建_PropertiesFilePath
*/
public void SetPropertiesFilePath(String _PropertiesFilePath) {
if (_PropertiesFilePath == "" && _PropertiesFilePath == null) {
System.out.println("No Setting PropertiesFilePath!");
} else {
this.PropertiesFilePath = _PropertiesFilePath;
}
}
![]()
// *****************************************************
/**
* 获取属性文件的数据库连接信息
*/
public void GetProperty() throws FileNotFoundException, IOException,
Exception {
Properties pro = new Properties();
try {
FileInputStream in = new FileInputStream(this.PropertiesFilePath);
pro.load(in);
this.driver = pro.getProperty("driver");
this.url = pro.getProperty("url");
this.user = pro.getProperty("user");
this.password = pro.getProperty("password");
} catch (FileNotFoundException e) {
throw new FileNotFoundException(
"FileNotFoundException Error Message :\n" + e.getMessage());
} catch (IOException e) {
throw new IOException("IOException Error Message :\n"
+ e.getMessage());
} catch (Exception e) {
throw new Exception("Other Error Message :\n" + e.getMessage());
}
}
![]()
/**
* 返回数据库连接
*/
public Connection GetConnection() throws Exception {
try {
Class.forName(this.driver); // 显式地加载 JDBC 驱动程序
this.Conn = DriverManager.getConnection(this.url, this.user,
this.password);
} catch (Exception e) {
throw new Exception("DataBase Connect Error Message :\n"
+ e.getMessage());
}
return this.Conn;
}
![]()
/**
* 返回一个数据集
*
* @param _Conn,数据库连接对象
* @param _SQLstr,SQL语句
*/
public ResultSet GetRs(Connection _Conn, String _SQLstr)
throws SQLException, Exception {
Statement st = null;
ResultSet rs = null;
try {
st = _Conn.createStatement();
rs = st.executeQuery(_SQLstr);
} catch (SQLException e) {
throw new SQLException("DataBase Error Message :\n"
+ e.getMessage());
} catch (Exception e) {
throw new Exception("Other Error Message :\n" + e.getMessage());
} finally {
}
return rs;
}
![]()
/** 输出方法 */
public void PrintProperties() {
System.out.println("Driver:" + this.driver);
System.out.println("Url:" + this.url);
System.out.println("User:" + this.user);
System.out.println("Password:" + this.password);
}
}
下面创建一个测试文件(Main.java),用来调用数据库访问类。
package ConnectionDemo;
![]()
import java.sql.*;
![]()
public class Main {
![]()
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DataAccess da = new DataAccess("DriverFile.properties");
da.GetProperty();
Connection conn = da.GetConnection();
![]()
ResultSet rs = da.GetRs(conn, "Select * from team");
while (rs.next()) {
String group1 = rs.getString("group1");
String team1 = rs.getString("team1");
![]()
System.out.println("\n组别:" + group1 + "\n球队:" + team1);
}
![]()
rs.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
![]()
}
对于数据库的访问,方法各异。如何选取,这个主要看实际需要啦!这里只是介绍相关方法,至于方案的选择还是在于开发需要!
开发环境:eclipse + SQL Server 2005,所需的sqljdbc下载(下载后解压) + JDK 5.0以上
首先导入sqljdbc.jar到项目中

选择 Import... ,弹出对话框

选择 Archive file 后,单击 Next 按钮

这样就完成了驱动文件的导入操作了!
之后的操作类似ODBC访问数据库文章中的内容,我在这里说明一下属性配置文件的内容吧!
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost;DatabaseName=WorldCup2006
user=ayumi
password=ayumiuser=你登录数据库的用户名
password=你登录数据库的用户密码
数据库访问类(DataAccess.java),这里的数据库访问类的内容基本上没有说明改变,可以继续使用之前文章介绍的,这里仍然给出该类的内容。
/*
* DataAccess.java 数据库访问类
*
* Created on 2008年06月15日
* Author ajayumi
* Description : 该类主要是用于做数据库操作,可以直接使用该类便可完成绝大部分的数据库访问操作
*/
package ConnectionDemo;
import java.sql.*;
import java.io.*;
import java.util.Properties;
public class DataAccess {
// *****************************************************
// 私有变量
/** 驱动连接串 */
private String driver = null;
/** jdbc:subprotocol:subname 形式的数据库 url */
private String url = null;
/** 数据库用户,连接是为该用户建立的 */
private String user = null;
/** 用户的密码 */
private String password = null;
/** 属性文件存放路径 */
private String PropertiesFilePath = null;
/** 数据库连接对象 */
private Connection Conn = null;
// *****************************************************
// *****************************************************
// 公共常量
public final String DefaultDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// *****************************************************
public DataAccess() {
}
/**
* 创建一个带参数的构造函数
*
* @param _driver,驱动连接串
* @param _url,jdbc:subprotocol:subname形式的数据库url
* @param _user,数据库用户,连接是为该用户建立的
* @param _password,用户的密码
*/
public DataAccess(String _driver, String _url, String _user,
String _password) {
this.SetDriver(_driver);
this.SetUrl(_url);
this.SetUser(_user);
this.SetPassword(_password);
}
/**
* 创建一个带参数的构造函数
*
* @param _PropertiesFilePath,属性文件存放路径
*/
public DataAccess(String _PropertiesFilePath) {
this.SetPropertiesFilePath(_PropertiesFilePath);
}
// *****************************************************
// 属性设置
/**
* 获取该类中的 driver
*
* @return driver
*/
public String getDriver() {
return this.driver;
}
/**
* 设置类中的 driver 为 _driver
*
* @param _driver,新建_driver
*/
public void SetDriver(String _driver) {
if (_driver == "" && _driver == null) {
System.out.println("No Setting Driver!");
} else {
this.driver = _driver;
}
}
/**
* 获取该类中的数据库 URL 的连接
*
* @return url
*/
public String getUrl() {
return this.url;
}
/**
* 设置类中的 url 为 _url
*
* @param _url,新建_url
*/
public void SetUrl(String _url) {
if (_url == "" && _url == null) {
System.out.println("No Setting Url!");
} else {
this.url = _url;
}
}
/**
* 获取该类中的数据库用户,连接是为该用户建立的
*
* @return user
*/
public String getUser() {
return this.user;
}
/**
* 设置类中的 user 为 _user
*
* @param _user,新建_user
*/
public void SetUser(String _user) {
if (_user == "" && _user == null) {
System.out.println("No Setting User!");
} else {
this.user = _user;
}
}
/**
* 获取该类中的数据库用户的密码
*
* @return password
*/
public String getPassword() {
return this.password;
}
/**
* 设置类中的 password 为 _password
*
* @param _password,新建_password
*/
public void SetPassword(String _password) {
if (_password == "" && _password == null) {
System.out.println("No Setting Password!");
} else {
this.password = _password;
}
}
/**
* 获取该类中的属性文件存放路径
*
* @return PropertiesFilePath
*/
public String getPropertiesFilePath() {
return this.PropertiesFilePath;
}
/**
* 设置类中的 PropertiesFilePath 为 _PropertiesFilePath
*
* @param _PropertiesFilePath,新建_PropertiesFilePath
*/
public void SetPropertiesFilePath(String _PropertiesFilePath) {
if (_PropertiesFilePath == "" && _PropertiesFilePath == null) {
System.out.println("No Setting PropertiesFilePath!");
} else {
this.PropertiesFilePath = _PropertiesFilePath;
}
}
// *****************************************************
/**
* 获取属性文件的数据库连接信息
*/
public void GetProperty() throws FileNotFoundException, IOException,
Exception {
Properties pro = new Properties();
try {
FileInputStream in = new FileInputStream(this.PropertiesFilePath);
pro.load(in);
this.driver = pro.getProperty("driver");
this.url = pro.getProperty("url");
this.user = pro.getProperty("user");
this.password = pro.getProperty("password");
} catch (FileNotFoundException e) {
throw new FileNotFoundException(
"FileNotFoundException Error Message :\n" + e.getMessage());
} catch (IOException e) {
throw new IOException("IOException Error Message :\n"
+ e.getMessage());
} catch (Exception e) {
throw new Exception("Other Error Message :\n" + e.getMessage());
}
}
/**
* 返回数据库连接
*/
public Connection GetConnection() throws Exception {
try {
Class.forName(this.driver); // 显式地加载 JDBC 驱动程序
this.Conn = DriverManager.getConnection(this.url, this.user,
this.password);
} catch (Exception e) {
throw new Exception("DataBase Connect Error Message :\n"
+ e.getMessage());
}
return this.Conn;
}
/**
* 返回一个数据集
*
* @param _Conn,数据库连接对象
* @param _SQLstr,SQL语句
*/
public ResultSet GetRs(Connection _Conn, String _SQLstr)
throws SQLException, Exception {
Statement st = null;
ResultSet rs = null;
try {
st = _Conn.createStatement();
rs = st.executeQuery(_SQLstr);
} catch (SQLException e) {
throw new SQLException("DataBase Error Message :\n"
+ e.getMessage());
} catch (Exception e) {
throw new Exception("Other Error Message :\n" + e.getMessage());
} finally {
}
return rs;
}
/** 输出方法 */
public void PrintProperties() {
System.out.println("Driver:" + this.driver);
System.out.println("Url:" + this.url);
System.out.println("User:" + this.user);
System.out.println("Password:" + this.password);
}
}
下面创建一个测试文件(Main.java),用来调用数据库访问类。
package ConnectionDemo;
import java.sql.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DataAccess da = new DataAccess("DriverFile.properties");
da.GetProperty();
Connection conn = da.GetConnection();
ResultSet rs = da.GetRs(conn, "Select * from team");
while (rs.next()) {
String group1 = rs.getString("group1");
String team1 = rs.getString("team1");
System.out.println("\n组别:" + group1 + "\n球队:" + team1);
}
rs.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
对于数据库的访问,方法各异。如何选取,这个主要看实际需要啦!这里只是介绍相关方法,至于方案的选择还是在于开发需要!




浙公网安备 33010602011771号