下面的这个程序就是我上午的问的有关数据库连接的.现在数据库我也给升级到sp4了.两中驱动的连接也测试成功了.但是下面的这个小程序还是不能正常的实现.
我的企业管理器中有数据库名为xsgl(学生管理),下面有个表是xsda(学生答案).这里用的就是纯java jdbc进行连接的.
但是elcipse老是在ConnectServer这个词上画红线.为什么会这样呢?
错误提示是:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ConnectServer cannot be resolved
Syntax error, insert ") Statement" to complete IfStatement
at StudentDataWindow.main(StudentDataWindow.java:180)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class StudentDataWindow extends JFrame implements ActionListener{
String title[]={ "班级","学号","姓名","性别","出生日期","团员否","家庭地址","简历"};
JTextField txtClassID = new JTextField(2);
JTextField txtNo = new JTextField(10);
JTextField txtName = new JTextField(3);
JTextField txtSex = new JTextField(3);
JTextField txtBirthDate = new JTextField(10);
JTextField txtIsMember = new JTextField(2);
JTextField txtAddress = new JTextField(30);
JTextArea txtResume = new JTextArea();
JButton next = new JButton("下一页");
JButton prev = new JButton("上一页");
JButton first = new JButton("首页");
JButton last = new JButton("尾页");
Statement stmt;
ResultSet rs;
StudentDataWindow(){
super("学生档案信息查看窗口");
setSize(450,395);
try{
stmt = ConnectServer.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from xsda");
Container con =getContentPane();
con.setLayout(new BorderLayout(0,8));
JPanel p[] = new JPanel[7];
for(int i =0;i<7;i++){
p[i]=new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtClassID);p[1].add(txtNo);p[2].add(txtName);
p[3].add(txtSex);p[4].add(txtBirthDate);
p[5].add(txtIsMember);p[6].add(txtAddress);
JPanel p1 = new JPanel(new GridLayout(7,1,0,8));
JScrollPane jp = new JScrollPane(txtResume,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jp.setPreferredSize(new Dimension(380,80));
for(int i=0;i<7;i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT,10,0));
p2.add(new JLabel(title[7]));p2.add(jp);
JPanel p3 =new JPanel();
p3.add(prev);p3.add(next);p3.add(first);p3.add(last);
con.add(p1,"North");con.add(p2,"Center");con.add(p3,"South");
next.addActionListener(this);
prev.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
rs.first();
loadData();
}catch(Exception e){ e.printStackTrace();}
setVisible(true);
}
void loadData(){
try{
txtNo.setText(rs.getString("no"));
txtClassID.setText(rs.getString("classID"));
txtName.setText(rs.getString("name"));
txtSex.setText(rs.getString("sex"));
txtBirthDate.setText(rs.getString("birthDate"));
txtAddress.setText(rs.getString("address"));
txtIsMember.setText(rs.getString("isMember"));
txtResume.setText(rs.getString("resume"));
}catch(SQLException e){ e.printStackTrace();}
}
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==next)rs.next();
else if(e.getSource()==prev) rs.previous();
else if(e.getSource()==first)rs.first();
else if(e.getSource()==last)rs.last();
loadData();
}catch(Exception ee){ }
}
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
Font font = new Font("JFrame",Font.PLAIN,14);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements()){
Object key = keys.nextElement();
if(UIManager.get(key) instanceof Font) UIManager.put(key, font);
}
if(!ConnectServer.conn("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xsgl","sa",""){
JOptionPane.showMessageDialog(null, "数据库连接不成功");
System.exit(0);
}
StudentDataWindow mainFrame = new StudentDataWindow();
}
}