📄 mdb10.java
字号:
}
}
/**
* Method connecion
*
* @param classfor
* 连接数据库
*/
private void connection() {
// TODO: Add your code here
try {
Class.forName(classfor);
conn = DriverManager.getConnection(constr, user, pwd);
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rec = st.executeQuery("select * from " + table);
first.setEnabled(true);
up.setEnabled(true);
down.setEnabled(true);
last.setEnabled(true);
add.setEnabled(true);
edit.setEnabled(true);
brow.setEnabled(true);
del.setEnabled(true);
mid.setEditable(true);
mname.setEditable(true);
maddr.setEditable(true);
mtel.setEditable(true);
mage.setEditable(true);
mxiaoxue.setEditable(true);
mzhongxue.setEditable(true);
mdaxue.setEditable(true);
nowrecfield.setEditable(true);
} catch (SQLException w) {
// JOptionPane.showMessageDialog(null,"sqlerror"+w.toString()+""+w.getErrorCode()+""+w.getSQLState());
JOptionPane.showMessageDialog(null, w.toString());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString() + ""
+ a.getMessage());
}
}
/**
* 浏览记录
*/
private void browrec() {
// TODO: Add your code here
list.setText("");
reccount = 0;
if (table == "y1") {
list.append("记录号\tID\t 姓名\t 年龄\t 地址\t 电话\n");
try {
rec = st.executeQuery("select * from " + table);
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");
}
rec.last();
nowrec = reccount;
recinformation.setText("共计" + String.valueOf(reccount)
+ "条记录,现在在" + String.valueOf(nowrec) + "条记录");
}
catch (SQLException f) {
JOptionPane.showMessageDialog(null, "sqlerror" + f.toString()
+ "" + f.getErrorCode() + "" + f.getSQLState());
} catch (Exception D) {
JOptionPane.showMessageDialog(null, " error" + D.toString()
+ "" + D.getMessage());
}
}
if (table == "y2") {
try {
rec = st.executeQuery("select * from " + table);
list.setText("");
rec = st.executeQuery("select * from " + table);
list.append("记录号\tID\t 小学\t 中学\t 大学\n");
reccount = 0;
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) + "\n");
}
rec.last();
nowrec = reccount;
recinformation.setText("共计" + String.valueOf(reccount)
+ "条记录,现在在" + String.valueOf(nowrec) + "条记录");
} catch (SQLException f) {
JOptionPane.showMessageDialog(null, "sqlerror" + f.toString()
+ "" + f.getErrorCode() + "" + f.getSQLState());
} catch (Exception D) {
JOptionPane.showMessageDialog(null, " error" + D.toString()
+ "" + D.getMessage());
}
}
}
/**
* 增加记录
*/
private void addrec() {
// TODO: Add your code here
if (table == "y1") {
try {
PreparedStatement ps = conn
.prepareStatement("INSERT INTO y1 VALUES(?,?,?,?,?)");
ps.setString(1, mid.getText().trim());
ps.setString(2, mname.getText().trim());
ps.setString(3, mage.getText().trim());
ps.setString(4, maddr.getText().trim());
ps.setString(5, mtel.getText().trim());
int m = ps.executeUpdate();
if (m == 1) {
JOptionPane.showMessageDialog(null, "添加成功");
browrec();
} else
JOptionPane.showMessageDialog(null, "添加失败");
ps.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());
}
}
if (table == "y2") {
try {
PreparedStatement ps = conn
.prepareStatement("INSERT INTO y2 VALUES(?,?,?,?)");
ps.setString(1, mid.getText().trim());
ps.setString(2, mxiaoxue.getText().trim());
ps.setString(3, mzhongxue.getText().trim());
ps.setString(4, mdaxue.getText().trim());
int m = ps.executeUpdate();
if (m == 1) {
JOptionPane.showMessageDialog(null, "添加成功");
browrec();
} else
JOptionPane.showMessageDialog(null, "添加失败");
ps.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());
}
}
}
/**
* 修改记录
*/
private void editrec() {
// TODO: Add your code here
// Connection
// con=DriverManager.getConnection("jdbc:odbc:"+odbc,user,pwd);
if (table == "y1") {
try {
PreparedStatement ps = conn
.prepareStatement("UPDATE "
+ table
+ " SET id=? ,name=? ,age=?, addr=?, tel=? WHERE id=?");
ps.setString(1, mid.getText().trim());
ps.setString(2, mname.getText().trim());
ps.setString(3, mage.getText().trim());
ps.setString(4, maddr.getText().trim());
ps.setString(5, mtel.getText().trim());
ps.setString(6, mid.getText().trim());
int m = ps.executeUpdate();
if (m == 1) {
JOptionPane.showMessageDialog(null, "修改成功");
browrec();
} else
JOptionPane.showMessageDialog(null, "修改失败,无此ID");
ps.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());
}
}
if (table == "y2") {
try {
PreparedStatement ps = conn
.prepareStatement("UPDATE "
+ table
+ " SET id=? ,xiaoxue=? ,zhongxue=?, daxue=? WHERE id=?");
ps.setString(1, mid.getText().trim());
ps.setString(2, mxiaoxue.getText().trim());
ps.setString(3, mzhongxue.getText().trim());
ps.setString(4, mdaxue.getText().trim());
ps.setString(5, mid.getText().trim());
int m = ps.executeUpdate();
if (m == 1) {
JOptionPane.showMessageDialog(null, "修改成功");
browrec();
} else
JOptionPane.showMessageDialog(null, "修改失败,无此ID");
ps.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());
}
}
}
/**
* 删除记录
*/
private void deleterec() {
// TODO: Add your code here
try {
PreparedStatement ps = conn
.prepareStatement("delete from y1 where id=?");
PreparedStatement ps2 = conn
.prepareStatement("delete from y2 where id=?");
ps.setString(1, mid.getText().trim());
ps2.setString(1, mid.getText().trim());
int m = ps.executeUpdate();
int n = ps2.executeUpdate();
if (m != 0 && n != 0) {
JOptionPane.showMessageDialog(null, "成功删除" + String.valueOf(m)
+ "条记录");
browrec();
} else
JOptionPane.showMessageDialog(null, "删除失败,无此ID");
ps.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());
}
}
/**
* 移动到首条
*
*/
private void tofirst() {
// TODO: Add your code here
try {
if (!rec.isFirst())
rec.first();
showrecord();
nowrec = reccount == 0 ? 0 : 1;
recinformation.setText("共计" + String.valueOf(reccount) + "条记录,现在在"
+ String.valueOf(nowrec) + "条记录");
} catch (SQLException w) {
JOptionPane.showMessageDialog(null, "sqlerror" + w.toString() + ""
+ w.getErrorCode() + "" + w.getSQLState());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString() + ""
+ a.getMessage());
}
}
/**
* 移动到上一条
*/
private void toup() {
// TODO: Add your code here
try {
rec.previous();
showrecord();
if (nowrec > 1)
nowrec--;
recinformation.setText("共计" + String.valueOf(reccount) + "条记录,现在在"
+ String.valueOf(nowrec) + "条记录");
} catch (SQLException w) {
JOptionPane.showMessageDialog(null, "sqlerror" + w.toString() + ""
+ w.getErrorCode() + "" + w.getSQLState());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString() + ""
+ a.getMessage());
}
}
/**
* 移动到下一条
*
*/
private void todown() {
// TODO: Add your code here
try {
rec.next();
showrecord();
if (nowrec < reccount)
nowrec++;
recinformation.setText("共计" + String.valueOf(reccount) + "条记录,现在在"
+ String.valueOf(nowrec) + "条记录");
} catch (SQLException w) {
JOptionPane.showMessageDialog(null, "sqlerror" + w.toString() + ""
+ w.getErrorCode() + "" + w.getSQLState());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString() + ""
+ a.getMessage());
}
}
/**
* 移动到末条
*/
private void tolast() {
// TODO: Add your code here
try {
if (!rec.isLast()) {
// rec.last();
rec.absolute(reccount);
showrecord();
nowrec = reccount;
recinformation.setText("共计" + String.valueOf(reccount)
+ "条记录,现在在" + String.valueOf(nowrec) + "条记录");
}
} catch (SQLException w) {
JOptionPane.showMessageDialog(null, "sqlerror" + w.toString() + ""
+ w.getErrorCode() + "" + w.getSQLState());
} catch (Exception a) {
JOptionPane.showMessageDialog(null, " error" + a.toString() + ""
+ a.getMessage());
}
}
/**
* 保存文件
*
*/
private void tofile() {
// TODO: Add your code here
try {
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("c:\\"));
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
String str = fc.getSelectedFile().getName();
while (str.indexOf(".") == -1) {
JOptionPane.showMessageDialog(null,
" 保存失败,请重新保存,输入文件扩展名\n例如.txt\t\t.bat.....");
return;
}
String filename = fc.getSelectedFile().getAbsolutePath();
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
String text = list.getText();
bw.write(text);
bw.flush();
bw.close();
}
} catch (Exception e) {
}
}
/**
* 打开文件
*
*
*/
private void tomdb() {
// TODO: Add your code here
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();
while (text != null) {
text = text + "\n";
list.append(text);
text = br.readLine();
}
br.close();
}
} catch (Exception e) {
}
}
/**
* 清除内容
*
*
*/
private void clear() {
// TODO: Add your code here
}
/**
* 修改颜色
*
*
*/
private void changecolor() {
// TODO: Add your code here
list.selectAll();
list.setBackground(JColorChooser.showDialog(null, null, null));
}
/**
* 监听改变内容
*
*
*/
class tablisener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
JTabbedPane my = (JTabbedPane) e.getSource();
int index = my.getSelectedIndex();
switch (index) {
case 0:
table = "y1";
break;
case 1:
table = "y2";
break;
case 2:
JOptionPane.showMessageDialog(null, "2");
break;
case 3:
JOptionPane.showMessageDialog(null, "3");
break;
case 4:
JOptionPane.showMessageDialog(null, "4");
break;
}
}
}
}// 全类结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -