//【例11.2】 连接指定数据库并获得数据库属性信息。
//【例11.3】 执行数据操纵的SQL语句。
// 数据库操作类
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.*;
import javax.swing.table.*;
public class DataBaseOperation
{
private Connection connection; //数据库连接对象
//构造方法,连接指定数据库。4个参数分别指定JDBC驱动程序、数据库URL、用户名和口令
public DataBaseOperation(String driver,String url, String user, String password)
throws ClassNotFoundException,SQLException
{
this.connection = null;
Class.forName(driver); //指定JDBC驱动程序
this.connection = DriverManager.getConnection(url,user,password); //返回数据库连接对象
}
public DataBaseOperation(String driver, String url) throws ClassNotFoundException,SQLException
{
this.connection = null;
Class.forName(driver);
this.connection = DriverManager.getConnection(url);
}
public void finalize() throws SQLException //析构方法,关闭数据库连接
{
this.connection.close();
}
public String getDBAbout() throws SQLException //获得所连接数据库的属性信息,返回字符串
{
String message = "";
DatabaseMetaData dbmd = this.connection.getMetaData();
message = "JDBC驱动程序:" + dbmd.getDriverName() +" "+ dbmd.getDriverVersion() + "\r\n" +
"JDBC URL:" + dbmd.getURL() + "\r\n" +
"数据库:" + dbmd.getDatabaseProductName() + "\r\n" +
"数据库版本:" + dbmd.getDatabaseProductVersion() + "\r\n"+
"用户名:" + dbmd.getUserName() + "\r\n";
return message;
}
//【例11.3】 执行数据操纵的SQL语句。
//执行数据更新的SQL语句,包括INSERT、UPDATE、DELETE语句
//执行成功返回所影响的行数,否则返回0
public int dataUpdate(String sql) throws SQLException
{
Statement statement = this.connection.createStatement();
int result = statement.executeUpdate(sql);
statement.close();
return result;
}
//执行数据查询的SELECT语句,参数table 指定表名,conditions 指定WHERE子句的查询条件,多个条件时用逻辑运算符连接
//执行成功返回数据结果集,否则返回null。
public void select(String sql) throws SQLException
{
Statement statement = this.connection.createStatement();
ResultSet resultset = statement.executeQuery(sql); //执行数据查询SELECT语句
ResultSetMetaData rsmd = resultset.getMetaData(); //返回元数据对象
int columnCount = rsmd.getColumnCount(); //获得列数
for(int j=1;j<=columnCount;j++)
System.out.print(rsmd.getColumnLabel(j)+" "); //获得列名
System.out.println();
while(resultset.next()) //从前向后访问每行
{
for(int j=1;j<=columnCount;j++) //获得每列值
System.out.print(resultset.getString(j)+" "); //获得当前行指定列的值
System.out.println();
}
resultset.close();
statement.close();
}
//实验11 按省份分类浏览student数据库中的stuinfo表。
//【例13.1】 输入并分类浏览参赛队信息。
//执行数据查询的SELECT语句,参数table 指定表名,conditions 指定WHERE子句的查询条件,多个条件时用逻辑运算符连接
//执行成功返回数据结果集,否则返回null。
public String[] selectDistinct(String table, String column) throws SQLException
{
String[] results=null;
if(table!=null && table!="")
{
String sql = "SELECT DISTINCT "+column+" FROM "+table+" ORDER BY "+column; //获得指定列不重复的值
Statement statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY); //设置结果集属性为可滚动、只读
ResultSet resultset = statement.executeQuery(sql); //执行数据查询SELECT语句
if (resultset!=null)
{
int rowCount=0;
while(resultset.next()) //获得结果集总行数
rowCount++;
results = new String[rowCount+1]; //按列数分配一维数组空间
results[0] = "全部";
resultset.beforeFirst(); //移动指针到第一行之前
int i=1;
while(resultset.next()) //获得每列数据
{
results[i] = resultset.getString(1); //获得当前行指定列的值
i++;
}
}
resultset.close();
statement.close();
}
else
throw new SQLException("表名不能为空。");
return results;
}
//执行包含集函数的数据查询SELECT语句
public int selectCount(String sql) throws SQLException
{
Statement statement = this.connection.createStatement();
ResultSet resultset = statement.executeQuery(sql); //执行数据查询SELECT语句
resultset.next();
int count = resultset.getInt(1);
resultset.close();
statement.close();
return count;
}
//执行数据查询的SELECT语句,参数table 指定表名,conditions 指定WHERE子句的查询条件,多个条件时用逻辑运算符连接
//执行成功返回数据结果集,否则返回null。
public void select(String table, String conditions, String sort_column, DefaultTableModel tableModel) throws SQLException
{
if(table!=null && table!="")
{
String sql = "SELECT * FROM " + table; //查询全部列时
if(conditions!=null && conditions!="")
sql +=" WHERE " + conditions; //增加查询条件
sql +=" ORDER BY "+ sort_column ; //按指定列的升序排序
// sql +=" ORDER BY '#1'" ; //默认按第1列的升序排序,SQL Server不支持#1
// System.out.println(sql);
Statement statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); //设置结果集属性为可滚动、只读
ResultSet resultset = statement.executeQuery(sql); //执行数据查询SELECT语句
if (resultset!=null)
{
int rowCount=0;
while(resultset.next()) //获得结果集总行数
rowCount++;
ResultSetMetaData rsmd = resultset.getMetaData(); //返回元数据对象
int columnCount = rsmd.getColumnCount(); //获得列数
tableModel.setRowCount(rowCount);
tableModel.setColumnCount(columnCount);
resultset.beforeFirst(); //移动指针到第一行之前
int i=0;
while(resultset.next()) //获得每列数据
{
for(int j=1;j<=columnCount;j++)
tableModel.setValueAt(resultset.getString(j), i, j-1);//获得当前行指定列的值
i++;
}
// dataModel.fireTableChanged(null);
}
else
tableModel.setRowCount(0);
resultset.close();
statement.close();
}
else
throw new SQLException("表名不能为空。");
}
}
/*
① 执行数据插入INSERT语句的方法
在DataBaseOperati