编程词典
明日图书
明日软件
用户注册
用户登录
站内
  当前位置:编程词典服务网 >> 编程词典社区论坛 >> 一切编译通过,可是数据...
共有回复7条  每页1条  第1页/共7页  首页  上页  下页  尾页
1
2
3
4
5
  · 一切编译通过,可是数据库还是连接不通,这是怎么回事??????

用户:  Piano
发帖:  22 篇
作品:  1 个
回复:  22 篇
技术分:  120 分
等级:  工程师助理
注册:  2010-11-25 18:30:32



发表于:2010-12-24 15:12:12
          楼主

以下是我的一个查询程序的源代码,一切都编译通过了,可是当我运行的时候,找不到数据库,无法连接数据库,我又检查了数据库的配置,是能测试通过的,数据库名和JDBC都是按照JAVA编程全能字典上的原样写上去的,请问为什么我的程序无法运行???????  请尽快帮我找出原因出来!!!!!

package com.zzk.frame;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Container;
import javax.swing.*;
import javax.swing.JDialog;
import javax.swing.WindowConstants;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JScrollBar;
import javax.swing.Box;
import javax.swing.JScrollPane;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
import javax.swing.JList;
import javax.swing.JTabbedPane;
import java.awt.Choice;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import com.swtdesigner.FocusTraversalOnArray; //导入java.sql包
import javax.swing.table.*;
import java.util.*;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;


public class QueryMessageFrame extends JDialog {
    //static
 static Connection con;   // 声明Connection对象
 static PreparedStatement sql;   // 声明Statement对象
 static ResultSet res;   // 声明ResultSet对象
 
   //Connection con; // 声明Connection对象
 
     public Connection getConnection(){                    //建立返回值为Connection的方法
                 try {
                       // 加载驱动
                       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                       System.out.println("数据库驱动加载成功");                                      // (1)
                 } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                 }
                 try {
                       // 连接数据库
                       con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Shanghai Conservatory of Music");                      // (2)
                       System.out.println("数据库连接成功");        //  sqlserver DSN_bookmanage   // localhost:1433    PC201011231112
                 } catch (SQLException e) {
                       e.printStackTrace();
                 }
                 return con;                                                       //按方法要求返回个Connection对象
           }
    
    
 private JTable table;
 private JTextField textField;
 private JComboBox comboBox;
 public String[] test={"u5E8Fu5217u53F7", "u59D3u540D", "u804Cu4E1A", "u5730u5740", "u5EA7u5F0Fu7535u8BDD", "u624Bu673A1", "u624Bu673A2", "u94A2u7434u54C1u724C", "u8C03u5F8Bu65F6u95F4"};              //定义下拉列表框中显示内容的数组;
 private JScrollPane scrollPane;
 private JTable table_1;
 private JTable table_2;
 private JTable table_3;
 
 public QueryMessageFrame() {
  super();
  setTitle("u67E5u8BE2u4FE1u606F");
  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);                // 设置对话框窗体的关闭方式
  //setModal(true);                                                                                   // 设置对话框窗体为模式对话框
  //getContentPane().setLayout(null);                                                           // 设置对话框窗体容器为绝对布局
  setBounds(5, 5, 1140, 820);    // 设置对话框窗体的显示位置和大小

  //WindowConstants
  final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.NORTH);
        final JLabel label = new JLabel("u67E5u8BE2u6761u4EF6uFF1A");
       
  label.setFont(new Font("宋体", Font.PLAIN, 14));
  label.setBounds(10, 10, 76, 15);
  panel.add(label);
  //getContentPane().add(label);
  
  comboBox = new JComboBox();                       // new MyComboBox()
  comboBox.setModel(new DefaultComboBoxModel(new String[] {"u5E8Fu5217u53F7", "u59D3u540D", "u804Cu4E1A", "u5730u5740", "u5EA7u5F0Fu7535u8BDD", "u624Bu673A1", "u624Bu673A2", "u94A2u7434u54C1u724C", "u8C03u5F8Bu65F6u95F4"}));
  comboBox.setToolTipText("u5E8Fu5217u53F7,rnu59D3u540D,rnu804Cu4E1A,rnu5730u5740,rnu5EA7u5F0Fu7535u8BDD,rnu624Bu673A1,rnu624Bu673A2,rnu94A2u7434u54C1u724C,rnu8C03u5F8Bu65F6u95F4");
  comboBox.setBounds(78, 7, 93, 21);
  panel.add(comboBox);
  
   //getContentPane().add(comboBox);
  
  textField = new JTextField();
  textField.setBounds(178, 7, 430, 21);
  //getContentPane().add(textField);
  textField.setColumns(40); 
  panel.add(textField);
 
  JButton button = new JButton("u67E5  u8BE2");
  button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    
    System.out.println(textField.getText());
    JOptionPane.showMessageDialog(null, textField.getText());
    System.out.println(comboBox.getSelectedIndex());
    System.out.println(comboBox.getSelectedItem());
    JOptionPane.showMessageDialog(null, comboBox.getSelectedItem());
   }
  });
  button.setFont(new Font("宋体", Font.PLAIN, 14));
  button.setBounds(618, 6, 93, 23);
  panel.add(button);
  //getContentPane().add(button);
 
  
  JButton button_1 = new JButton("u67E5u8BE2u6240u6709u540Du5F55");
  button_1.setFont(new Font("宋体", Font.PLAIN, 14));
  button_1.setBounds(732, 6, 126, 23);
  panel.add(button_1);
  //getContentPane().add(button_1);
 
  button_1.addActionListener(new ActionListener() {
   public void actionPerformed(final ActionEvent e) {
   
    QueryMessageFrame c = new QueryMessageFrame();                     // 创建本类对象
    Connection con = c.getConnection();                                     // 调用连接数据库方法
     try {
    Statement sql = con.prepareStatement("select '编号','姓名','地址',’电话','品牌', '型号','琴号','购买日期' from DAI_2006$");                           // 查询数据库
     //Statement    PreparedStatement
     ResultSet  res = sql.executeQuery("select '编号','姓名','地址',’电话','品牌', '型号','琴号','购买日期' from DAI_2006$");               // 执行SQL语句
        //ResultSet           '编号','姓名','地址',’电话'
     String[] columnNames = { "编号", "姓名", "家庭住址", "电话号码", "品牌", "型号","琴号","购买日期"};                                              // 定义表格列名数组
    //, "品牌", "型号","琴号","购买日期","免费调律次数", "调律日期","收费"
    Vector columnNameV = new Vector();                              // 创建表格列名向量
    
    for (int column = 0; column < columnNames.length; column++) {
                columnNameV.add(columnNames[column]);                      // 将数组中的数据添加到列名向量
          }
           
          Vector tableValueV = new Vector();
          // 创建表格的数据向量
          while (res.next()) {
                Vector rowV = new Vector();                                                             // 创建行值向量
                for (int column = 0; column < columnNames.length; column++) {
                      rowV.add(res.getString(column));                                      // 为行值向量赋值
                }
                tableValueV.add(rowV);                                                                     // 将行值向量中的数据添加到数据向量
          }
         
          JTable table = new MTable(tableValueV, columnNameV); 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);                                // 关闭表格列的自动调整功能
          table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);                // 选择模式为单选
          table.setSelectionBackground(Color.YELLOW);                                          // 被选择行的背景色为黄色
          table.setSelectionForeground(Color.RED);                                                 // 被选择行的前景色(文字颜色)为红色
          table.setRowHeight(30);     
          // 表格的行高为30像素  

          TableColumnModel tcm = table.getColumnModel();
          TableColumn tc = tcm.getColumn(1);
          tc.setPreferredWidth(80);
          tc.setMinWidth(50);
          tc.setMaxWidth(500);
    
          TableColumn tc_1 = tcm.getColumn(2);
          tc_1.setPreferredWidth(70);
          tc_1.setMinWidth(50);
          tc_1.setMaxWidth(500);
    
          TableColumn tc_2 = tcm.getColumn(3);
          tc_2.setPreferredWidth(350);
          tc_2.setMinWidth(50);
          tc_2.setMaxWidth(500);
    
          TableColumn tc_3 = tcm.getColumn(4);
          tc_3.setPreferredWidth(230);
          tc_3.setMinWidth(50);
          tc_3.setMaxWidth(500);
  
          scrollPane.setViewportView(table);                                                           // 将表格添加到滚动面板的视图
    
          res.close();
          sql.close();
          con.close();
     } 
        
      catch (Exception a) {
            a.printStackTrace();
         }
     
   }
  });
  
  scrollPane = new JScrollPane(table);                                         // 创建滚动面板
        
  // scrollPane.setViewportView(table);                                                           // 将表格添加到滚动面板的视图
       
        getContentPane().add(scrollPane, BorderLayout.CENTER);                           // 将滚动面板添加到窗体容器的中央
     
  JButton button_2 = new JButton("u9000  u51FA");
  button_2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
   QueryMessageFrame.this.dispose();
  
   }
  });
  button_2.setBounds(880, 6, 93, 23);
  panel.add(button_2);
  //getContentPane().add(button_2);
 
 }       
       
 private class MTable extends JTable {                                                                       // 实现自己的表格类
        public MTable(Vector rowData, Vector columnNames) {
              super(rowData, columnNames);                                                         // 调用父类的构造方法
        }
        @Override
        public JTableHeader getTableHeader() {                                                    // 定义表格头
              JTableHeader tableHeader = super.getTableHeader();                            // 获得表格头对象
              tableHeader.setReorderingAllowed(false);                                            // 设置表格列不可重排
              // 获得表格头的单元格对象
              tableHeader.setPreferredSize(new Dimension(2130, 32));    //设置表头大小
              tableHeader.setFont(new Font("宋体", Font.BOLD, 15));
              DefaultTableCellRenderer cellRender= (DefaultTableCellRenderer) tableHeader.getDefaultRenderer();
              // 设置列名居中显示
              cellRender.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
              return tableHeader;
        }
        @Override
        public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {        // 定义单元格
              // 获得表格的单元格对象
              DefaultTableCellRenderer cr = (DefaultTableCellRenderer) super.getDefaultRenderer(columnClass);
              cr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);            // 设置单元格内容居中显示
              return cr;
        }
        @Override
        public boolean isCellEditable(int row, int column) {                                  // 表格不可编辑
              return false;
        }
}
    
 /**
  * @param args
  */
 
 public static void main(String[] args) {
  
       new QueryMessageFrame().setVisible(true);                                                // 创建系统登录窗体类的实例,并显示登录窗体
 }
 protected void initDataBindings() {
  BeanProperty<JTable, Boolean> jTableBeanProperty = BeanProperty.create("enabled");
  BeanProperty<JScrollPane, Boolean> jScrollPaneBeanProperty = BeanProperty.create("visible");
  AutoBinding<JTable, Boolean, JScrollPane, Boolean> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, table, jTableBeanProperty, scrollPane, jScrollPaneBeanProperty);
  autoBinding.bind();
 }
}

 

 

 

以下是运行后给出的错误信息:

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at com.zzk.frame.QueryMessageFrame.getConnection(QueryMessageFrame.java:60)
 at com.zzk.frame.QueryMessageFrame$2.actionPerformed(QueryMessageFrame.java:144)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Shanghai Conservatory of Music
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at com.zzk.frame.QueryMessageFrame.getConnection(QueryMessageFrame.java:67)
 at com.zzk.frame.QueryMessageFrame$2.actionPerformed(QueryMessageFrame.java:144)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
 at com.zzk.frame.QueryMessageFrame$2.actionPerformed(QueryMessageFrame.java:146)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
本帖赏分:0 分   回复数:7 篇     
修改   删除   引用   回复
  · 明日科技:回复

用户:  aha2130
发帖:  0 篇
作品:  0 个
回复:  62 篇
技术分:  154 分
等级:  工程师助理
注册:  2010-06-25 10:10:25



回复于:2010-12-24 15:56:31
         1 楼
读者您好:请您导入数据库的驱动文件~
修改   删除   引用
共有回复7条  每页1条  第1页/共7页  首页  上页  下页  尾页
1
2
3
4
5
吉林省明日科技有限公司提供技术支持. 
Copyright © 1999-2013 吉林省明日科技有限公司. 吉ICP备10002740
当前时区 GMT+8, 今天是 
刷新页面 - 关于我们 - 设为首页