| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5828 人关注过本帖
标题:JOptionPane.showMessageDialog的问题
只看楼主 加入收藏
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
我也是跟着书上来写的,呵呵!!!
[CODE]

import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import sun.jdbc.odbc.JdbcOdbcDriver;

public class DatabaseUtilities {

static String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
static String dbName = "JDBC";
static String urlRoot = "jdbc:odbc:";
private ActionListener exceptionListener = null;

public DatabaseUtilities() {
registerDriver();
}

public void setDatabaseName(String dbName) {
this.dbName = dbName;
}

public void registerDriver() {
try {
Class.forName(jdbcDriver);
DriverManager.registerDriver(new JdbcOdbcDriver());

} catch(ClassNotFoundException e) {
reportException(e.getMessage());

} catch(SQLException e) {
reportException(e.getMessage());
}
}

public String login(String tableName, String dbuser, String dbpassword)
{
String message = null;
Vector user = new Vector();
Vector password = new Vector();
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";

try {
Connection con = DriverManager.getConnection(url);
Statement stms = con.createStatement();
ResultSet rs = stms.executeQuery(SQLCommand);
while(rs.next()) {
user.add(rs.getString("name").trim());
password.add(rs.getString("password").trim());
}
} catch(SQLException sqlExc) {
reportException(sqlExc.getMessage());
}

if (!user.contains(dbuser) && !password.contains(dbpassword))
{
message = "你输入的用户名不存在";
}
else
{
if (!password.contains(dbpassword))
{
message = "你输入的密码有误";
}else
{
message = "你以管理员"+dbuser+"的身份登陆";
}
}
return message;
}

public void execute(String SQLCommand) {
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
stmt.execute(SQLCommand);
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
}

public void excute(String[] SQLCommand) {
String url = urlRoot + dbName;
try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
for(int i = 0; i < SQLCommand.length; i++) {
stmt.execute(SQLCommand[i]);
}
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
}

public String[] getColumnNames(String tableName) {
Vector dataSet = new Vector();
String[] columnNames = null;
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";

try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();

columnNames = new String[md.getColumnCount()];
for(int i = 0; i < columnNames.length; i++) {
columnNames[i] = md.getColumnLabel(i+1);
}
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
return columnNames;
}

public String[] getColumnNamesUsingQuery(String SQLCommand) {

Vector dataSet = new Vector();
String[] columnNames = null;
String url = urlRoot + dbName;

try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();

columnNames = new String[md.getColumnCount()];
for(int i = 0; i < columnNames.length; i++) {
columnNames[i] = md.getColumnLabel(i+1);
}
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
return columnNames;
}

public String[] getDataTypes(String tableName) {
Vector dataSet = new Vector();
String[] dataTypes = null;
String url = urlRoot + dbName;
String SQLCommand = "SELECT * FROM " + tableName + ";";

try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLCommand);
ResultSetMetaData md = rs.getMetaData();

dataTypes = new String[md.getColumnCount()];
for(int i = 0; i < dataTypes.length; i++) {
dataTypes[i] = md.getColumnTypeName(i+1);
}
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
return dataTypes;
}

public Vector executeQuery(String SQLQuery) {

Vector dataSet = new Vector();
String url = urlRoot + dbName;

try {
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLQuery);
ResultSetMetaData md = rs.getMetaData();

int nColumns = md.getColumnCount();
while(rs.next()) {
Vector rowData = new Vector();
for(int i = 1; i <= nColumns; i++) {
rowData.addElement(rs.getObject(i));
}
dataSet.addElement(rowData);
}
con.close();

} catch(SQLException e) {
reportException(e.getMessage());
}
return dataSet;
}
public void setExceptionListener(ActionListener exceptionListener) {
this.exceptionListener = exceptionListener;
}

private void reportException(String exception) {
if(exceptionListener != null) {
ActionEvent evt = new ActionEvent(this, 0, exception);
exceptionListener.actionPerformed(evt);
}
else {
System.err.println(exception);
}
}
}
[/CODE]

其它的一些方法,我写的程序用没用到。。。我只是把我的代码,插入到原来书上例题中去了。。。
个人认为这些方法应该可以包括操作数据库所有的操作了 

[此贴子已经被作者于2007-1-14 13:01:48编辑过]


开开心心的过&玩每一天!!!!
2007-01-14 12:58
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
得分:0 
谢谢楼主的奉献精神.共同学习!

2007-01-14 13:01
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
呵呵

开开心心的过&玩每一天!!!!
2007-01-14 13:13
cr123321
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-1-13
收藏
得分:0 
请问怎么调用一个有下拉选项类似ShowMessageDialog的信息框
2007-01-14 18:55
中文名
Rank: 1
等 级:新手上路
威 望:1
帖 子:45
专家分:0
注 册:2006-12-12
收藏
得分:0 

哪种下拉

2007-01-14 19:31
cr123321
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-1-13
收藏
得分:0 
类似与ShowInputDialog那样的
不过是一个下拉框的列表来选择

2007-01-14 20:27
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
showshowInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

这个方法可以帮你

可惜不是你,陪我到最后
2007-01-14 21:03
中文名
Rank: 1
等 级:新手上路
威 望:1
帖 子:45
专家分:0
注 册:2006-12-12
收藏
得分:0 
Object message
用一个JComboBox类型的变量传进去不就行了么
2007-01-14 21:44
tempnetbar
Rank: 2
等 级:新手上路
威 望:4
帖 子:582
专家分:4
注 册:2004-5-5
收藏
得分:0 
无语……

相信勤能补拙! 喜欢用好用的就永远学不到有用的。
2007-01-15 12:02
cr123321
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-1-13
收藏
得分:0 
谢谢

[此贴子已经被作者于2007-1-15 19:41:49编辑过]


2007-01-15 19:36
快速回复:JOptionPane.showMessageDialog的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040186 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved