📄 mdb10.java
字号:
* 删除记录
*/
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r) {
deleterec();
showrecord();
}
});
/**
* 获取表设计信息
*
*/
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
mation.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r) {
try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Connection
// conn=DriverManager.getConnection("jdbc:odbc:"+odbc,user,pwd);
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery("select * from y1");
rsmd = rec.getMetaData();
list.append("name\t length\t type\t \n");
for (int t = 1; t <= rsmd.getColumnCount(); t++) {
list.append(rsmd.getColumnName(t) + "\t"
+ rsmd.getColumnDisplaySize(t) + "\t"
+ rsmd.getColumnTypeName(t) + "\n");
}
rec.close();
st.close();
// conn.close();
} catch (SQLException w) {
JOptionPane.showMessageDialog(null, "sqlerror"
+ w.toString() + "" + w.getErrorCode() + ""
+ w.getSQLState());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString()
+ "" + a.getMessage());
}
}
});
/**
*
* 浏览全部记录
*/
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
brow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent x) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:"
+ odbc, user, pwd);
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery("select * from " + table);
reccount = 0;
list.setText("");
if (table == "y1") {
list.append("记录号\tID\t 姓名\t 年龄\t 地址\t 电话\n");
while (rec.next()) {
reccount++;
list.append(String.valueOf(reccount) + "\t"
+ rec.getString(1) + "\t"
+ rec.getString(2) + "\t"
+ rec.getString(3) + "\t"
+ rec.getString(4) + "\t"
+ rec.getString(5) + "\n");
}
}
if (table == "y2") {
list.append("记录号\tID\t 小学\t 中学\t 大学\t\n");
while (rec.next()) {
reccount++;
list.append(String.valueOf(reccount) + "\t"
+ rec.getString(1) + "\t"
+ rec.getString(2) + "\t"
+ rec.getString(3) + "\t"
+ rec.getString(4) + "\t\n");
}
}
rec.close();
st.close();
conn.close();
} catch (SQLException f) {
JOptionPane.showMessageDialog(null, "sqlerror"
+ f.toString() + "" + f.getErrorCode() + ""
+ f.getSQLState());
} catch (Exception D) {
JOptionPane.showMessageDialog(null, " error" + D.toString()
+ "" + D.getMessage());
}
}
});
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
nowrecfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = nowrecfield.getText();
if (Integer.parseInt(text) > reccount
|| Integer.parseInt(text) < 1)
{
JOptionPane.showMessageDialog(null, " 无效位置");
return;
}
try {
rec.absolute(Integer.parseInt(text));
showrecord();
recinformation.setText("共计" + String.valueOf(reccount)
+ "条记录,现在在"
+ String.valueOf(Integer.parseInt(text)) + "条记录");
} catch (Exception rece) {
}
}
});
// ///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 写文件
*
*/
tofile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent x) {
tofile();
}
});
/**
*
* 读入文件
*/
tomdb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent x) {
tomdb();
}
});
/**
*
* 清除文本文档
*/
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent r) {
list.setText("");
}
});
/**
*
* 获取数据源
*/
// ///////////////////////////////////////////////////
m1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
odbc = JOptionPane.showInputDialog(null, "请输入数据源", "");
if (odbc.length() == 0)
odbc = "yang";
constr = "jdbc:odbc:" + odbc;
classfor = "sun.jdbc.odbc.JdbcOdbcDriver";
connection();
browrec();
showrecord();
}
});
/**
*
* 连接SQL("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 在此注意,
* forName方法的参数字符串必须完全相同于以上内容,大小写是区分的, 其实这个串就是驱动类的完整名称:包名+类名。 4、获得一个连接
* 在操作数据库之前,要先获得与数据库的一个连接, 使用如下代码格式: DriverManager.getConnection(连接字符串,
* 登录用户名, 登录密码); 例:
* DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;
* DatabaseName=pubs", "sa", "");
* 在此处关键的是连接字符串的内容,localhost部分即服务器的名字,可以更改;1433部分为SQL
* Server使用的端口号,根据实际情况修改即可;
* DatabaseName即为要连接的数据库的名字,在此注意DatabaseName之前的是分号,而不是冒号。
*/
m20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
classfor = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
constr = "jdbc:microsoft:sqlserver://" + host + ":" + port
+ ";DatabaseName=" + mdb;
// constr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=yang,sa,
// ";
connection();
}
});
/**
*
* 获取主机
*/
// ///////////////////////////////////////////////////
m21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
host = JOptionPane.showInputDialog(null, "请输入服务器名称", "");
if (host.length() == 0)
host = "localhost";
}
});
/**
*
* 获取端口号
*/
// ///////////////////////////////////////////////////
m22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
port = JOptionPane.showInputDialog(null, "请输端口号", "");
if (port.length() == 0)
port = "1433";
}
});
/**
*
* 获取数据库
*/
// ///////////////////////////////////////////////////
m23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mdb = JOptionPane.showInputDialog(null, "请输入数据库(不带扩展名)", "");
if (mdb.length() == 0)
mdb = "yang.mdb";
}
});
/**
*
* 获取用户
*/
// ///////////////////////////////////////////////////
m24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
user = JOptionPane.showInputDialog(null, "请输入用户", "");
if (user.length() == 0)
user = "sa";
}
});
/**
*
* 获取密码
*/
// ///////////////////////////////////////////////////
m25.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pwd = JOptionPane.showInputDialog(null, "请输入密码", "");
if (pwd.length() == 0)
pwd = "";
}
});
/**
* 读取配置文件
*/
m5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("c:\\"));
if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String filename = jfc.getSelectedFile()
.getAbsolutePath();
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
// JOptionPane.showMessageDialog(null,"text"+text);
if (text != null) {
StringTokenizer tokens = new StringTokenizer(text,
"@");
odbc = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"odbc"+odbc);
user = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"user"+user);
pwd = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"pwd"+pwd);
constr = "jdbc:odbc:" + odbc;
classfor = "sun.jdbc.odbc.JdbcOdbcDriver";
connection();
}
br.close();
}
} catch (Exception ee) {
JOptionPane.showMessageDialog(null, "不是合格的配置文件");
}
}
});
/**
* 帮助文档
*/
m6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame help;
help = new JFrame("帮助文档");
JTextArea helptext = new JTextArea();
helptext
.append("******************配置数据源YANG,数据库db1.mdb在解压缩目录下\r\n\n\n");
helptext
.append("******************下部为文本编辑器,可以将记录导入到文件中,文件必须加扩展名\r\n\n\n");
helptext
.append("******************配置文件格式,数据源@用户名@密码,某项为空,必须用双引号表示\r\n");
helptext
.append("\t例如YANG@双引号@双引号\t注意SQL数据源典型用法是YANG@SA@双引号\r\n");
helptext
.append("******************使用SQL数据库必须给SQL2000打补丁,默认使用1433端口\r\n");
helptext.setEditable(false);
help.getContentPane().add(helptext);
help.pack();
help.setVisible(true);
help.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
}
});
}
});
/**
* 颜色菜单
*/
colormenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changecolor();
}
});
/**
* 清除内容
*/
clearmenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.setText("");
}
});
/**
* 打开菜单
*/
tomdbmenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tomdb();
}
});
/**
* 保存菜单
*/
tofilemenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tofile();
}
});
/**
*
* 修改文本字体
*/
// ///////////////////////////////////////////////////
ffont.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
list.selectAll();
list.setFont(new Font(ffont.getSelectedItem().toString(),
Font.ITALIC, Integer.parseInt(String.valueOf(fsize
.getSelectedItem()))));
}
});
/**
*
* 修改文本大小
*/
// ///////////////////////////////////////////////////
fsize.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
list.selectAll();
list.setFont(new Font(ffont.getSelectedObjects().toString(),
Font.ITALIC, Integer.parseInt(String.valueOf(fsize
.getSelectedItem()))));
}
});
/**
*
* 修改文本颜色
*/
// //////////////////////
fcolor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changecolor();
}
});
/**
* 监听右键
*/
list.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (e.isMetaDown()) {
popmenu.show(list, e.getX(), e.getY());
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
/**
* 读取默认配置文件
*
*/
try {
FileReader fr = new FileReader("readme.txt");
BufferedReader br = new BufferedReader(fr);
String text = br.readLine();
// JOptionPane.showMessageDialog(null,"text"+text);
if (text != null) {
StringTokenizer tokens = new StringTokenizer(text, "@");
odbc = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"odbc"+odbc);
user = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"user"+user);
pwd = tokens.nextToken();
// JOptionPane.showMessageDialog(null,"pwd"+pwd);
constr = "jdbc:odbc:" + odbc;
classfor = "sun.jdbc.odbc.JdbcOdbcDriver";
connection();
}
br.close();
} catch (Exception ee) {
//JOptionPane.showMessageDialog(null, "无法找到配置文件readme.txt");
}
}// ///////////////////////////////////////////////////////////////////////////////////////////////////////构造函数结束
/**
* 主函数
*
*/
// ///////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String agrs[]) {
mdb10 a = new mdb10();
}
/**
* Method showrecord 显示记录
*/
private void showrecord() {
// TODO: Add your code here
try {
mid.setText(rec.getString(1));
mname.setText(rec.getString(2));
mage.setText(rec.getString(3));
maddr.setText(rec.getString(4));
mtel.setText(rec.getString(5));
} catch (Exception e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -