大家帮找一个毛病,为什么窗体中没有调用数据库的内容?
本人初学者,大家帮找一下存在什么问题,为什么窗体中没有调用数据库的内容?
我找了半天也没找到呀,数据库用的是mysql,能够正常连接呀!可就是搞不懂
==========================================================================================================
[code=Java][/code]
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class Example10_9 extends JFrame implements ActionListener{
/**
* @
*/
public static Connection connectMYSql(String url,String username,String password){
Connection con = null;
try{
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();return null;
}
try{
con = DriverManager.getConnection(url,username,password);
}catch(SQLException e){
e.printStackTrace();return null;
}
return con;
}
String title[] = {"考号","姓名","成绩","地址","简历"};
JTextField txtNo = new JTextField(8);
JTextField txtName = new JTextField(10);
JTextField txtScore = new JTextField(3);
JTextField txtAddr = new JTextField(30);
JTextArea txtResume = new JTextArea();
JButton prev = new JButton("前一个");
JButton next = new JButton("后一个");
JButton first = new JButton("第一个");
JButton last = new JButton("最后一个");
Statement sql;
ResultSet rs;
Example10_9(Connection connect){//构造方法
super("考生信息查看窗口");
setSize(450,350);
try{
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new BorderLayout(0,6));
JPanel p[] = new JPanel[4];
for(int i = 0;i < 4;i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT,8,0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4,1,0,8));
JScrollPane jsp = new JScrollPane(txtResume,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize(new Dimension(300,60));
for(int i = 0;i < 4;i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
}catch(Exception e){e.printStackTrace();}
setVisible(true);
}
public void modifyRecord(Connection connect){
String stuNo = (String)JOptionPane.showInputDialog(null,"请输入考生考号","输入考号对话框",JOptionPane.PLAIN_MESSAGE,null,null,"");
try{
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new BorderLayout(0,6));
JPanel p[] = new JPanel[4];
for(int i = 0;i < 4;i++){
p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT,8,0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPanel(new GridLayout(4,1,0,8));
JScrollPane jsp = new JScrollPane(txtResume,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize(new Dimension(300,60));
for(int i = 0;i < 4;i++){p1.add(p[i]);}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
p2.add(new JLabel(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
readRecord();
}catch(Exception e){e.printStackTrace();}
setVisible(true);
}
boolean readRecord(){
try{
txtNo.setText(rs.getString("考号"));
txtName.setText(rs.getString("姓名"));
txtScore.setText(rs.getString("成绩"));
txtAddr.setText(rs.getString("地址"));
txtResume.setText(rs.getString("简历"));
}catch(SQLException e){e.printStackTrace();return false;}
return true;
}
public void actionPerformed(ActionEvent e){
try{
if(e.getSource() == prev)rs.previous();
else if(e.getSource() == next) rs.next();
else if(e.getSource() == first) rs.first();
else if(e.getSource() == last) rs.last();
readRecord();
}catch(Exception e2){}
}
public static void main(String[] args) {
Connection connect = null;
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame",Font.PLAIN,14);
if((connect = connectMYSql("jdbc:mysql://127.0.0.1:3306/teacher","weichao","123456")) == null){
JOptionPane.showMessageDialog(null,"数据库连接失败!");
System.exit(-1);
}
new Example10_9(connect);//创建对象
}
}
[code=Java][/code]
[解决办法]
package test;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.sql.*;public class AAA extends JFrame implements ActionListener { /** * @ */ public static Connection connectMYSql(String url, String username, String password) { Connection con = null; try { // 加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); return null; } try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); return null; } return con; } String title[] = { "考号", "姓名", "成绩", "地址", "简历" }; JTextField txtNo = new JTextField(8); JTextField txtName = new JTextField(10); JTextField txtScore = new JTextField(3); JTextField txtAddr = new JTextField(30); JTextArea txtResume = new JTextArea(); JPanel p[] = new JPanel[4]; JButton prev = new JButton("前一个"); JButton next = new JButton("后一个"); JButton first = new JButton("第一个"); JButton last = new JButton("最后一个"); Statement sql; ResultSet rs; AAA(Connection connect) {// 构造方法 super("考生信息查看窗口"); setSize(450, 350); try { sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sql.executeQuery("SELECT * FROM ksInfo"); Container con = getContentPane(); con.setLayout(new BorderLayout(0, 6));// JPanel p[] = new JPanel[4]; for (int i = 0; i < 4; i++) { p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0)); p[i].add(new JLabel(title[i])); } p[0].add(txtNo); p[1].add(txtName); p[2].add(txtScore); p[3].add(txtAddr); JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8)); JScrollPane jsp = new JScrollPane(txtResume, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jsp.setPreferredSize(new Dimension(300, 60)); p1.setVisible(true) ; this.add(p1) ; for (int i = 0; i < 4; i++) { p1.add(p[i]); p[i].setVisible(true) ; } JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0)); p2.setVisible(true) ; p2.add(new JLabel(title[4])); p2.add(jsp); JPanel p3 = new JPanel(); p3.add(prev); p3.add(next); p3.add(first); p3.add(last); prev.addActionListener(this); next.addActionListener(this); first.addActionListener(this); last.addActionListener(this); rs.first(); readRecord(); System.out.println(3) ;// while(rs.next()){// System.out.println(111) ;// readRecord();// break ;// } System.out.println(this.txtNo.getText()) ; } catch (Exception e) { e.printStackTrace(); } setVisible(true); } public void modifyRecord(Connection connect) { String stuNo = (String) JOptionPane.showInputDialog(null, "请输入考生考号", "输入考号对话框", JOptionPane.PLAIN_MESSAGE, null, null, ""); try { sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sql.executeQuery("SELECT * FROM ksInfo"); Container con = getContentPane(); con.setLayout(new BorderLayout(0, 6)); JPanel p[] = new JPanel[4]; for (int i = 0; i < 4; i++) { p[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 8, 0)); p[i].add(new JLabel(title[i])); p[i].setVisible(true) ; } p[0].add(txtNo); p[1].add(txtName); p[2].add(txtScore); p[3].add(txtAddr); System.out.println(1) ; JPanel p1 = new JPanel(new GridLayout(4, 1, 0, 8)); JScrollPane jsp = new JScrollPane(txtResume, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); jsp.setPreferredSize(new Dimension(300, 60)); for (int i = 0; i < 4; i++) { p1.add(p[i]); } JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0)); p2.add(new JLabel(title[4])); p2.add(jsp); JPanel p3 = new JPanel(); p3.add(prev); p3.add(next); p3.add(first); p3.add(last); System.out.println(2) ; prev.addActionListener(this); next.addActionListener(this); first.addActionListener(this); last.addActionListener(this);// rs.first(); System.out.println(3) ; while(rs.next()){ readRecord(); break ; } System.out.println(this.txtNo.getText()) ; } catch (Exception e) { e.printStackTrace(); } setVisible(true); } public boolean readRecord() { try { System.out.println(rs.getString("no")) ; txtNo.setText(rs.getString("no")); txtName.setText(rs.getString("name")); txtScore.setText(rs.getString("score")); txtAddr.setText(rs.getString("addr")); txtResume.setText(rs.getString("oo")); System.out.println(6) ; } catch (SQLException e) { e.printStackTrace(); return false; } return true; } public void actionPerformed(ActionEvent e) { try { if (e.getSource() == prev) rs.previous(); else if (e.getSource() == next) rs.next(); else if (e.getSource() == first) rs.first(); else if (e.getSource() == last) rs.last(); readRecord(); } catch (Exception e2) { } } public static void main(String[] args) { Connection connect = null; JFrame.setDefaultLookAndFeelDecorated(true); Font font = new Font("JFrame", Font.PLAIN, 14); if ((connect = connectMYSql("jdbc:mysql://localhost:3306/javaee", "root", "root")) == null) { JOptionPane.showMessageDialog(null, "数据库连接失败!"); System.exit(-1); } new AAA(connect);// 创建对象 }}