补补补补1月TAT
回想起1月的事情,最让我印象深刻又痛苦的当然是JAVA最后的大作业了
枉我悬赏100圆豆都解决不了问题 桑心
废话不多说 ,我绝对不承认我是在凑字数,~\(≧▽≦)/~啦啦啦
下面是正题
写了2个包,一个是Main 一个是Swing
Main里面只有2个java,一个是与数据库建立连接的,一个是主界面运行的
//Main.java
public class Main
{
public static void main(String[] args)
{
Login l=new Login();
l.setLocationRelativeTo(null);//窗口居中
l.setVisible(true);//窗口显示
}
}
//DBlink.java
package Main;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBlink
{
public Connection con=null;
public DBlink()
{
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1434;DatabaseName=BusSystem";
String userName="sa"; //默认用户名
String userPwd="000000"; //密码
try{
Class.forName(driverName);
con=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接成功!"); //如果连接成功,控制台输出“连接成功!”
}catch(Exception e)
{
System.out.println("连接失败!");
e.printStackTrace();
}
}
}
接下来是Swing包里的内容
图形界面还是挺容易搭建的,感觉和画图出不多,= =
关键是写好监听器就好了,当然这个也挺容易的
eg:
JButton button_2 = new JButton();//一个button
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) //鼠标点击时完成的动作
{
JOptionPane.showMessageDialog(null, "感谢您的使用!", "对话框",
JOptionPane.PLAIN_MESSAGE);//显示对话框,提示感谢您的使用
System.exit(0);//退出系统
}
});
button_2.setText("\u9000\u51FA");//退出的文本
当然还有窗口的显示和隐藏
setVisible(false);//当前窗口的隐藏
Window lo = new Window();
lo.setLocationRelativeTo(null);//在屏幕正中间显示
lo.setVisible(true);//新窗口的显示
到了最蛋疼的部分,TAT
就是与数据库连接后对数据进行查询,添加,删除,修改,并显示在Jtable的表格里
//查询
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
sp = "bus";
String[] data = new String[6];
DBlink a = new DBlink();
PreparedStatement p = null;//命令对象
ResultSet rs = null;//结果集对象
String s[] = { "NUMBER", "TYPE", "ENGINE NUMBER", "SEATING CAPACITY", "ROUTE", "AD" };
String sql = "select * from bus";
DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();
int numT;//表格已有数据的行数
numT = model.getRowCount();//获取当前已有行数
while (numT > 0) //如果是全体刷新表格需要移除之前的所有数据行
{
model.removeRow(0);
numT--;
}
int i;
for (i = 0; i < s.length; i++) {
model.setColumnIdentifiers(s);
}
try {
p = a.con.prepareStatement(sql);//命令对象
rs = p.executeQuery();
while (rs.next()) {
data[0] = rs.getString("number").trim();
data[1] = rs.getString("type").trim();
data[2] = rs.getString("engine_numbe").trim();
data[3] = rs.getString("seating_capacity").trim();
data[4] = rs.getString("route").trim();
data[5] = rs.getString("ad").trim();
model.addRow(data);
}
jTable1.setModel(model);//将修改后的model传回给JTable*
a.con.close();
p.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//添加
public void add() {
DBlink a = new DBlink();
PreparedStatement p = null;//命令对象
try {
this.number=Integer.parseInt(jTextField6.getText());
this.type = jTextField1.getText();
this.engine_numbe = Integer.parseInt(jTextField2.getText());
this.seating_capacity = Integer.parseInt(jTextField3.getText());
this.route = Integer.parseInt(jTextField4.getText());
this.ad = jTextField5.getText();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
return;
}
Object[] value = { "是", "否", "取消" };
int result = JOptionPane.showOptionDialog(null, "确定添加", "警告", 2, 2,
null, value, value[0]);
if (result == 0) {
String sql2 = "insert into bus(number,type,engine_numbe,seating_capacity,route,ad) values(?,?,?,?,?,?)";
try {
p = a.con.prepareStatement(sql2);//命令对象
p.setInt(1, number);
p.setString(2, type);
p.setInt(3, engine_numbe);
p.setInt(4, seating_capacity);
p.setInt(5, route);
p.setString(6, ad);
p.execute();
JOptionPane.showMessageDialog(null, "添加成功");
a.con.close();
p.close();
} catch (SQLException e) {
//JOptionPane.showMessageDialog(null, "数据库连接错误!", "错误",
// JOptionPane.WARNING_MESSAGE);
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
}
}
refresh();
}
//修改
public void update() {
DBlink a = new DBlink();
PreparedStatement p = null;//命令对象
ResultSet rs = null;//结果集对象
try {
this.number = Integer.parseInt(jTextField6.getText());
this.type = jTextField1.getText();
this.engine_numbe = Integer.parseInt(jTextField2.getText());
this.seating_capacity = Integer.parseInt(jTextField3.getText());
this.route = Integer.parseInt(jTextField4.getText());
this.ad = jTextField5.getText();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
return;
}
Object[] value = { "是", "否", "取消" };
int result = JOptionPane.showOptionDialog(null, "确定修改此", "警告", 2, 2,
null, value, value[0]);
if (result == 0) {
String sql1 = "select * from bus where number=?";
String sql2 = "update bus set type=?,engine_numbe=?,seating_capacity=?,route=?,ad=? where number=?";
try {
p = a.con.prepareStatement(sql1);//命令对象
p.setInt(1, number);
rs = p.executeQuery();
if (rs.next() && rs.getRow() > 0) {
p = a.con.prepareStatement(sql2);//命令对象
p.setString(1, type);
p.setInt(2, engine_numbe);
p.setInt(3, seating_capacity);
p.setInt(4, route);
p.setString(5, ad);
p.setInt(6, number);
p.execute();
} else {
JOptionPane.showMessageDialog(null, "不存在您要修改的车辆不存在", "错误",
JOptionPane.WARNING_MESSAGE);
}
a.con.close();
p.close();
rs.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库连接错误!", "错误",
JOptionPane.WARNING_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
}
}
refresh();
}
//删除
public void delete() {
jTextField1.setEditable(true);
DBlink a = new DBlink();
PreparedStatement p = null;//命令对象
ResultSet rs = null;//结果集对象
try {
this.number = Integer.parseInt(jTextField6.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
return;
}
if (jTextField1.equals(null)) {
JOptionPane.showMessageDialog(this, "序号不能为空", "错误",
JOptionPane.WARNING_MESSAGE);
return;
}
Object[] value = { "是", "否", "取消" };
int result = JOptionPane.showOptionDialog(null, "确定删除此", "警告", 2, 2,
null, value, value[0]);
if (result == 0) {
String sql1 = "select * from bus where number=?";
String sql2 = "delete from bus where number=?";
try {
p = a.con.prepareStatement(sql1);//命令对象
p.setInt(1, number);
rs = p.executeQuery();
if (rs.next() && rs.getRow() > 0) {
p = a.con.prepareStatement(sql2);//命令对象
p.setInt(1, number);
p.execute();
JOptionPane.showMessageDialog(null, "删除成功!");
} else {
JOptionPane.showMessageDialog(null, "不存在您要删除的车辆", "错误",
JOptionPane.WARNING_MESSAGE);
}
a.con.close();
p.close();
rs.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库连接错误!", "错误",
JOptionPane.WARNING_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "输入数据格式错误!", "错误",
JOptionPane.WARNING_MESSAGE);
}
}
refresh();
}
一月份的内容到此结束!