2 qq 28412711 qq_28412711 于 2016.01.20 02:26 提问

数据库连接不上,一直报错

各位哥哥姐姐,能帮我一下吗?我java数据库连接不上。
我的是mysql 5.0 java 4.2
数据库是5.1.36
网上说把jar包放在lib,我不知道放哪里?
希望各位能指教一下我新手。谢谢·急!!
package denglu;

import gongyong.DaoConn;
import gongyong.SaveUserInfo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class loginchuankou extends JFrame{
public DaoConn daoconn=new DaoConn();

private JButton guanbi=new JButton("退出");
private JButton Queding=new JButton("登录");

private JPasswordField mimakuang=new JPasswordField(20);
private JTextField yonghumingkuang=new JTextField(20);

private JLabel yonghubiaoqian=new JLabel();

private JLabel mimabiaoqian=new JLabel();



public loginchuankou(){
    setTitle("系统登录");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    setBounds(200,200,300,180);
//  setResizable(false);
    JPanel dengluchuan=new JPanel();
    getContentPane().add(dengluchuan,BorderLayout.CENTER);
    setVisible(true);




    yonghubiaoqian.setText("用户名");
    dengluchuan.add(yonghubiaoqian);

    dengluchuan.add(yonghumingkuang);


    dengluchuan.add(Queding);

    mimabiaoqian.setText("密码");
    dengluchuan.add(mimabiaoqian);

    dengluchuan.add(yonghumingkuang);

    dengluchuan.add(mimakuang);

    dengluchuan.add(guanbi);
    //键盘
    guanbi.addMouseListener(new MouseAdapter(){

        public void mouseClicked(final MouseEvent e){
            System.exit(0);
        }

            });

    Queding.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent evt) {            
            Queding(evt);         
                        }   
        });

    /////

}

protected void Queding(ActionEvent evt) {
    Connection conn = null;           //????
    Statement st = null;                
    ResultSet rs = null;   
    try {
        conn =(Connection) daoconn.getConn();           
        st = (Statement) conn.createStatement();    

        String user=yonghumingkuang.getText().trim();
        String pw=new String(mimakuang.getPassword()).trim();

        String sql="  select * from user where username='"+ user + "' and password= '"+ pw + "'  ";

        rs=(ResultSet) st.executeQuery(sql);

        if(rs.next() && rs.getString("username")!=null){
            SaveUserInfo.setuserName(user);
            SaveUserInfo.setpassword(pw);
            System.out.print("成功");
        }else{
            System.out.print("失败");
        }
    } catch (Exception e) {

        e.printStackTrace();
    }finally{
    }

}//Queding

}//gouzao

报错如下:
java.lang.ClassNotFoundException: com.mysql.jdbc.Drive
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at gongyong.DaoConn.getConn(DaoConn.java:8)
at denglu.loginchuankou.Queding(loginchuankou.java:105)
at denglu.loginchuankou$2.actionPerformed(loginchuankou.java:89)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

10个回答

Evankaka
Evankaka   Ds   Rxr 2016.01.20 12:26
已采纳

右键-》属性后,在弹出框中选择
图片说明
战鼓最右边的红框,然后选择你的jar

qq_28412711
qq_28412711 我试过不行,会报错。
接近 2 年之前 回复
caozhy
caozhy   Ds   Rxr 2016.01.20 02:35

java.lang.ClassNotFoundException: com.mysql.jdbc.Drive
驱动问题,需要导入jar包
eclipse中,展开lib目录,右键添加

qq_28412711
qq_28412711 你好,我找不到lib文件在哪,我工程下好像没有这个文件。
接近 2 年之前 回复
ramostear
ramostear   2016.01.20 06:10

把mysql的驱动包拷贝到你工程的lib文件夹下

qq_28412711
qq_28412711 你好,我的工程文件下没有这个文件目录
接近 2 年之前 回复
rui888
rui888   Ds   Rxr 2016.01.20 09:12

没有lib 文件夹吗?
sql驱动没有加载到。
java.lang.ClassNotFoundException: com.mysql.jdbc.Drive

Royal_lr
Royal_lr   Ds   Rxr 2016.01.20 09:43

java项目当然没有lib文件夹了,将驱动包丢到src目录下,,右键jar包-->buildpath-->add to buildpath

Royal_lr
Royal_lr 回复qq_28412711: 都行,只要把jar包加进去就行了
接近 2 年之前 回复
qq_28412711
qq_28412711 直接放进src文件?我在构建配置添加JAR没看到放进去的JAR包。。但是按添加外部JAR就有。不过不知道行不?我试一试
接近 2 年之前 回复
xiaomoxn
xiaomoxn   2016.01.20 09:56

java项目可以手动建一个文件夹取名lib,然后将所需jar放入该文件夹,
右击项目名——》build path——》configure build path——》选择libraries——》add jars——选择你的jar包,点击OK即可。

qq_28412711
qq_28412711 自己创建lib?
接近 2 年之前 回复
shasiqq
shasiqq   2016.01.20 10:01

有链接数据库操作的项目 最好建成web项目 建成web项目后 问题就迎刃而解了!

qq_28412711
qq_28412711 请问怎么连啊
接近 2 年之前 回复
qq_28412711
qq_28412711   2016.01.20 11:08

我的版本是5.1.36 驱动包也是5.1.36,应该不关事吧?

cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2016.01.20 13:37

添加jar包,没有成功

qq_28412711
qq_28412711   2016.01.22 23:13

问题已经解决,谢谢各位

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!