马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
新手上路,请多关照。
在做Java中的数据库部分的习题,如何将要打印出来的表放到弹出窗口呢?自己试了好多种方法都失败了,只能请求大神帮忙了。下面有习题的要求和自己写的拙劣的代码,还希望大神不怕麻烦,指点一下。
窗口代码:package com.cdd.frame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.cdd.Newframe.Newframe;
import com.cdd.student.Student;
import com.cdd.zsgc.Delete;
import com.cdd.zsgc.Insert;
import com.cdd.zsgc.Select1;
import com.cdd.zsgc.Select2;
import com.cdd.zsgc.Update;
public class Frame1 {
public static void main(String[] args) {
Frame2 A = new Frame2();
A.visible_Frame2();
}
}
/**
*
窗口设计及实现按钮的功能
*/
class Frame2 extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JPanel jp6 = new JPanel();
JPanel jp7 = new JPanel();
JPanel jp8 = new JPanel();
JPanel jp9 = new JPanel();
JPanel jp10 = new JPanel();
TextField te1 = new TextField(10);
TextField te2 = new TextField(3);
TextField te3 = new TextField(20);
TextField te4 = new TextField(10);
TextField te5 = new TextField(10);
TextField te6 = new TextField(10);
JComboBox<String> jco1 = new JComboBox<String>();
JButton bu1 = new JButton("添加");
JButton bu2 = new JButton("取消");
JButton bu3 = new JButton("删除");
JButton bu4 = new JButton("取消");
JButton bu5 = new JButton("更新");
JButton bu6 = new JButton("取消");
JButton bu7 = new JButton("查询全部");
JButton bu8 = new JButton("条件查询");
JButton bu9 = new JButton("取消");
JLabel lab1 = new JLabel("name:");
JLabel lab2 = new JLabel("sex:");
JLabel lab3 = new JLabel("age:");
JLabel lab4 = new JLabel("college:");
JLabel lab5 = new JLabel("请输入你要删除信息人的名字: 如:张三");
JLabel lab6 = new JLabel("请输入你要更新的信息及信息的所属学生: 如:sex='男' 张三 age=18 赵四");
JLabel lab7 = new JLabel("全部查询及查询age>=20的学生信息");
public void visible_Frame2() {
setSize(number.b2, number.b1);
setTitle("数据库编程");
setResizable(true);
setVisible(true);
Toolkit c1 = getToolkit();
Dimension c2 = c1.getScreenSize();
int c3 = c2.width;
int c4 = c2.height;
setLocation((c3 - number.b1) / 2, (c4 - number.b2) / 2);
setLayout(new GridLayout(10, 1));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jco1.addItem("男 ");
jco1.addItem("女 ");
add(jp1); jp1.add(lab1);jp1.add(te1);jp1.setBackground(Color.CYAN);
add(jp2); jp2.add(lab2);jp2.add(jco1);jp2.setBackground(Color.CYAN);
add(jp3); jp3.add(lab3);jp3.add(te2);jp3.setBackground(Color.CYAN);
add(jp4); jp4.add(lab4);jp4.add(te3);jp4.setBackground(Color.CYAN);
add(jp5); jp5.add(bu1);jp5.add(bu2);jp5.setBackground(Color.CYAN);
add(jp6); jp6.add(lab5);jp6.add(te4);jp6.setBackground(Color.lightGray);
add(jp7); jp7.add(bu3);jp7.add(bu4);jp7.setBackground(Color.lightGray);
add(jp8); jp8.add(lab6);jp8.add(te5);jp8.add(te6);jp8.setBackground(Color.orange);
add(jp9); jp9.add(bu5);jp9.add(bu6);jp9.setBackground(Color.orange);
add(jp10);jp10.add(lab7);jp10.add(bu7);jp10.add(bu8);
jp10.add(bu9);jp10.setBackground(Color.pink);
// 下面实现按钮的功能
bu1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Student student = new Student();
student.setName(te1.getText());
student.setAge(Integer.parseInt(te2.getText()));
student.setSex((String) jco1.getSelectedItem());//getSelectedItem()返回JComboBox()中的
//参数,以对象的形式返回
student.setCollege(te3.getText());
Insert insert = new Insert();
insert.Insert_info(student);
}
});
bu2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
bu3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Student student=new Student();
student.setName(te4.getText());
Delete delete=new Delete();
delete.Delete_info(student);
}
});
bu4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
bu5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String set_info=te5.getText();
String name=te6.getText();
Update update=new Update();
update.Updete_info(set_info, name);
}
});
bu6.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
bu7.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Newframe newframe=new Newframe();
newframe.Visible_new();
Select1 selete= new Select1();
selete.Select1_info();
}
});
bu8.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Select2 selete2= new Select2();
selete2.Select2_info();
}
});
bu9.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
}
class number {
final static int b1 = 700;
final static int b2 = 700;
}
数据库驱动链接:
package com.cdd.jdbc;
import java.sql.*;
public class GetConn {
public Connection conn = null; // 创建Connection对象
// 获取数据库连接方法
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载数据库驱动
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root";
String passWord = "123456";
conn = DriverManager.getConnection(
url, user, passWord);
// getConnection()方法参数分别指定连接数据库的URL、用户名和密码
if (conn != null) {
System.out.println("数据库连接成功");
}
} catch (Exception e) {
e.printStackTrace();
}// 异常处理
return conn;// 返回Connection对象
}
}
新窗口设计代码:package com.cdd.Newframe;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 弹出窗口的设计
*/
public class Newframe extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
TextArea A1=new TextArea(10,10);
TextArea A2=new TextArea(10,10);
public void Visible_new(){
setSize(number1.b2, number1.b1);
setTitle("数据库编程");
setResizable(true);
setVisible(true);
Toolkit c1 = getToolkit();
Dimension c2 = c1.getScreenSize();
int c3 = c2.width;
int c4 = c2.height;
setLocation((c3 - number1.b1) / 2, (c4 - number1.b2) / 2);
setLayout(new GridLayout(2, 1));
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
add(A1);add(A2);
A1.setFont(new Font("微软雅黑",Font.PLAIN,20));
A2.setFont(new Font("微软雅黑",Font.PLAIN,20));
}
}
class number1 {
final static int b1 = 600;
final static int b2 = 600;
}
表查询代码:
package com.cdd.zsgc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.cdd.jdbc.GetConn;
public class Select1 {
GetConn getconn = new GetConn();
Connection conn = getconn.getConnection();
public void Select1_info() {
String sql = "select *from student2";
try {
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery(sql);
while (resultset.next()) {
String name = resultset.getString("name");
String sex = resultset.getString("sex");
int age = resultset.getInt("age");
String college = resultset.getString("college");
System.out.println("name: " + name + " sex: " + sex
+ " age: " + age + " college: " + college);//要打印到新窗口
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
其他功能的代码没有发,感觉会更乱,麻烦大神了。
在 select1.java 文件中,我将 Select1_info() 的 void 返回值改为了 String,添加如下语句:
我们再返回到 Newframe.java 这个文件中,我在这里面加了一个方法
该方法作用是设置 TextArea 中文本的内容,调用其 setText(String s) 方法,传入参数类型为 String
,我们再返回到 Frame1.java 这个文件中,在 bu7 这个按扭单击事件代码处我添加如下语句:
将 Select1_info() 方法的返回值作为 setTextArea() 方法的参数,然后再传到 setText() 方法中,从而将
打印出来的表放到了弹出的窗口中。
这里还有一个问题,我想留给你自已解决,思路我都已经在上面说的很清楚了,什么问题呢?当数据库这个
表中存在多条数据时,我上面这个方法只能输出表最后一条,因为覆盖了,正如上面鱼油所说,可以用集合来
保存返回的的值,然后一次性将其添加到 TextArea 里。
|