首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.sql.sqlexception:没有合适的驱动程序(数据库链接)

问java.sql.sqlexception:没有合适的驱动程序(数据库链接)
EN

Stack Overflow用户
提问于 2013-12-06 20:16:20
回答 2查看 102关注 0票数 0

当我运行我的应用程序时,我收到了这样的消息(java.sql.sqlexception:没有合适的驱动程序(数据库链接)),我在13个小时内搜索了13个小时,尝试了10多个解决方案,所以..这是我的连接代码

代码语言:javascript
复制
                       package khaledbayzav0.pkg1;

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  import java.sql.Statement;
  import javax.swing.*;

 public class MySQLconn {
 public static Connection mkmbhs(){

           try{

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/fuchs","root","mkmbhs");
                 Statement stmt = (Statement) con.createStatement();
     }
             catch(SQLException e){

                JOptionPane.showMessageDialog(null, e);

}
        return null;
   }
 }

这是数据库的操作(我的按钮和字段(jFrame) )

代码语言:javascript
复制
     package khaledbayzav0.pkg1;
     import java.awt.event.* ;
     import java.awt.* ;
     import java.sql.*;
     import javax.swing.JOptionPane;
     import javax.swing.*;
     public class NewCustomers extends javax.swing.JFrame {

Connection con = null;
ResultSet rs = null;
PreparedStatement pst = null;


public NewCustomers() {
    initComponents();
    con = MySQLconn.mkmbhs();
}


@SuppressWarnings("unchecked")

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    jLabel5 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    name = new javax.swing.JTextField();
    tele = new javax.swing.JTextField();
    adrs = new javax.swing.JTextField();
    sv = new javax.swing.JTextField();
    add = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jLabel6 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "New Customer Form", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 11))); // NOI18N

    jLabel5.setText("Supervisor:");

    jLabel4.setText("Address:");

    jLabel3.setText("Telphone:");

    jLabel2.setText("Customer Name:");

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel2)
                .addComponent(jLabel3)
                .addComponent(jLabel4)
                .addComponent(jLabel5))
            .addGap(79, 79, 79)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(sv, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addComponent(adrs)
                .addComponent(tele)
                .addComponent(name))
            .addContainerGap(55, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(tele, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(adrs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(sv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel5)))
            .addContainerGap(23, Short.MAX_VALUE))
    );

    add.setText("ADD");
    add.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addActionPerformed(evt);
        }
    });

    jButton2.setText("BACK");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jLabel6.setFont(new java.awt.Font("Blackadder ITC", 0, 12)); // NOI18N
    jLabel6.setForeground(new java.awt.Color(204, 0, 0));
    jLabel6.setText("Designed By Mkmbhs");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addGap(58, 58, 58)
            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
            .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(40, 40, 40))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(230, Short.MAX_VALUE)
            .addComponent(jLabel6)
            .addGap(219, 219, 219))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(32, 32, 32)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(add)
                .addComponent(jButton2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
            .addComponent(jLabel6)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    close();
    CustomersSelection CS = new CustomersSelection();
    CS.setVisible(true);
}                                        

private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
    try {

        String sql = "INSERT INTO customerinof(CustomerName, CustomerAddress, CustomerTelephone, CustomerSupervisor) values (?,?,?,?)";
        pst = con.prepareStatement(sql);

        pst.setString(1, name.getText());
        pst.setString(2, adrs.getText());
        pst.setString(3, tele.getText());
        pst.setString(4, sv.getText());


        pst.execute();
        JOptionPane.showMessageDialog(null, "New Customer added to your table ");
    }
    catch (Exception e){

        JOptionPane.showMessageDialog(null, e);
    }
}                                   


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewCustomers().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton add;
private javax.swing.JTextField adrs;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField name;
private javax.swing.JTextField sv;
private javax.swing.JTextField tele;
// End of variables declaration                   

private void close() {
    WindowEvent closeW = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closeW);
}

   }
EN

回答 2

Stack Overflow用户

发布于 2013-12-06 20:19:23

将您的mysql driver jar (mysql-connector.jar)放在服务器库文件夹中。这需要存在于classpath中,以便与数据库交互。

票数 1
EN

Stack Overflow用户

发布于 2013-12-06 20:20:35

您正在加载驱动程序类而不是

代码语言:javascript
复制
Class.forName("com.mysql.jdbc.Driver");  

看一看MySQL docs

您需要在类路径中使用mysql-connector jar来加载Driver类。

你可以从MySQL site下载

相关链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20423709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档