关于java.lang.ClassNotFoundException的mysql数据库连接程序 数据库mysqljava异常jar 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是LZ,补充一下,我试验过,进行数据库的“增”和“改”操作可行,但是出现异常,但是那个根据id查询就没成功,也出现了异常。 是不是还是少jar包或者jar包有问题 会不会是jar包版本问题呢?或者你用了什么框架中间有冲突啊?您还是把调用语句也贴出来吧,光看这个看不出来什么啊 好的,我贴一下调用的程序,增和改操作能成功,jar包也可能有问题吗?//更新操作都用preparedStatement,都要将确保数据库关闭package com.lww.UserProject.DAO.impl;import com.lww.UserProject.DAO.*;import com.lww.UserProject.jdbc.*;import com.lww.UserProject.vo.*;import java.sql.*;import java.util.*;public class IUserDAOImpl implements IUserDAO { //声明dbc,conn private DatabaseConnection dbc=null; private Connection conn=null; public IUserDAOImpl(){ this.dbc=new DatabaseConnection(); this.conn=this.dbc.getConnection(); } @Override /* *进行“增”的操作 * */ public boolean doCreate(User user) throws Exception { // TODO Auto-generated method stub boolean flag=false; PreparedStatement pstmt = null; String sql="INSERT INTO user (name,sex,birthday) VALUES (?,?,?)"; try{ pstmt=conn.prepareStatement(sql); pstmt.setString(1,user.getName()); pstmt.setString(2, user.getSex()); pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime())); if(pstmt.executeUpdate()>0){//至少更新了一次 flag=true; } } catch(Exception e){ throw e; } finally{ if(pstmt != null){ try{ pstmt.close(); } catch(Exception e){ throw e; } } try{ conn.close(); } catch(SQLException e){ throw e; } } return flag;} //进行更新操作 @Override public boolean doUpdate(User user) throws Exception { boolean flag=false; PreparedStatement pstmt = null; String sql="UPDATE user SET name=?,sex=?,birthday=? WHERE id=?"; try{ pstmt=conn.prepareStatement(sql); pstmt.setString(1,user.getName()); pstmt.setString(2, user.getSex()); pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime())); pstmt.setInt(4, user.getId()); if(pstmt.executeUpdate()>0){//至少更新了一次 flag=true; } } catch(Exception e){ throw e; } finally{ if(pstmt != null){ try{ pstmt.close(); } catch(Exception e){ throw e; } } try{ conn.close(); } catch(SQLException e){ throw e; } } return flag; } //进行删除操作 @Override public Boolean doDelete(int id) throws Exception { boolean flag=false; PreparedStatement pstmt = null; String sql="DELETE FROM user WHERE id=?"; try{ pstmt=conn.prepareStatement(sql); pstmt.setInt(1, id); if(pstmt.executeUpdate()>0){//至少更新了一次 flag=true; } } catch(Exception e){ throw e; } finally{ if(pstmt != null){ try{ pstmt.close(); } catch(Exception e){ throw e; } } try{ conn.close(); } catch(SQLException e){ throw e; } } return flag; } //通过关键词,进行查询 public List<User> findALL(String keyWord) throws Exception { List<User> all=new ArrayList<User>(); PreparedStatement pstmt = null; String sql="SELECT id,name,sex,birthday FROM user WHERE name LIKE ?,sex LIKE ?,birthday LIKE ?"; try{ pstmt=conn.prepareStatement(sql); pstmt.setString(1, "%"+keyWord+"%"); pstmt.setString(2, "%"+keyWord+"%"); pstmt.setString(3, "%"+keyWord+"%"); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ User user=new User(); user.setId(rs.getInt(1)); user.setName(rs.getString(2)); user.setSex(rs.getString(3)); user.setBirthday(rs.getDate(4)); all.add(user); } rs.close(); } catch(Exception e){ throw e; } finally{ if(pstmt != null){ try{ pstmt.close(); } catch(Exception e){ throw e; } } conn.close(); } return all; } //通过id,查询 public User findByID(int id) throws Exception{ User user=new User(); PreparedStatement pstmt=null; String sql="SELECT id,name,sex,birthday FROM user WHERE id =?"; try{ pstmt=conn.prepareStatement(sql); ResultSet rs=pstmt.executeQuery(); while(rs.next()){ user.setId(rs.getInt(1)); user.setName(rs.getString(2)); user.setSex(rs.getString(3)); user.setBirthday(rs.getDate(4)); } rs.close(); } catch(Exception e){ throw e; } finally{ if(pstmt != null){ pstmt.close(); } dbc.close(); } return user; }} com.mysql.jdbc.Driver吧,如果你确认jar包添加好了,打开那个jar包,看看里面有没有org.gjt.mm.mysql这个类 你好,是有org.gjt.mm.mysql这个包,里面有Driver.class 你好,本人新手,换一个jar包就是指在网上下一个,然后再add这个jar包? 你好,是有org.gjt.mm.mysql这个包,里面有Driver.class那应该是org.gjt.mm.mysql.driver这么写吧 你好,是有org.gjt.mm.mysql这个包,里面有Driver.class那应该是org.gjt.mm.mysql.driver这么写吧你好,谢谢你,的确是你说的这样,我之前不用加Driver也行的,不过加了的确成功了,分就给你了,也谢谢其他有评论的人。 关于页面跳转的问题!!! &和&&分别是什么意思,一样吗? 如何能够在 JTable 单元格中显示一个超链接信息?在线等,答出立即给分 学完了J2SE,接下来如何做才好,请教各位达人~ 枚举 打开文件时,路径有空格该怎么办 请问jbx2005 图形界面在design下设计时控件一切换面板就看不到了,只有少数几个能正常看到,运行正常 请问如何解决?在线等待.... java 处理中文的问题!急! 关于class打成jar包后,app访问路径问题,大家来讨论!给分!!! JAVA2的哪个方面比较有前途?? 关于ArrayList的疑问 ListIterator实现逆序输出时没反应?
package com.lww.UserProject.DAO.impl;import com.lww.UserProject.DAO.*;
import com.lww.UserProject.jdbc.*;
import com.lww.UserProject.vo.*;import java.sql.*;
import java.util.*;public class IUserDAOImpl implements IUserDAO {
//声明dbc,conn
private DatabaseConnection dbc=null;
private Connection conn=null;
public IUserDAOImpl(){
this.dbc=new DatabaseConnection();
this.conn=this.dbc.getConnection();
} @Override
/*
*进行“增”的操作
*
*/
public boolean doCreate(User user) throws Exception {
// TODO Auto-generated method stub
boolean flag=false;
PreparedStatement pstmt = null;
String sql="INSERT INTO user (name,sex,birthday) VALUES (?,?,?)";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2, user.getSex());
pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
if(pstmt.executeUpdate()>0){//至少更新了一次
flag=true;
}
}
catch(Exception e){
throw e;
}
finally{
if(pstmt != null){
try{
pstmt.close();
}
catch(Exception e){
throw e;
}
}
try{
conn.close();
}
catch(SQLException e){
throw e;
}
}
return flag;
}
//进行更新操作
@Override
public boolean doUpdate(User user) throws Exception {
boolean flag=false;
PreparedStatement pstmt = null;
String sql="UPDATE user SET name=?,sex=?,birthday=? WHERE id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2, user.getSex());
pstmt.setDate(3,new java.sql.Date(user.getBirthday().getTime()));
pstmt.setInt(4, user.getId());
if(pstmt.executeUpdate()>0){//至少更新了一次
flag=true;
}
}
catch(Exception e){
throw e;
}
finally{
if(pstmt != null){
try{
pstmt.close();
}
catch(Exception e){
throw e;
}
}
try{
conn.close();
}
catch(SQLException e){
throw e;
}
}
return flag;
}
//进行删除操作
@Override
public Boolean doDelete(int id) throws Exception {
boolean flag=false;
PreparedStatement pstmt = null;
String sql="DELETE FROM user WHERE id=?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
if(pstmt.executeUpdate()>0){//至少更新了一次
flag=true;
}
}
catch(Exception e){
throw e;
}
finally{
if(pstmt != null){
try{
pstmt.close();
}
catch(Exception e){
throw e;
}
}
try{
conn.close();
}
catch(SQLException e){
throw e;
}
}
return flag;
}
//通过关键词,进行查询
public List<User> findALL(String keyWord) throws Exception {
List<User> all=new ArrayList<User>();
PreparedStatement pstmt = null;
String sql="SELECT id,name,sex,birthday FROM user WHERE name LIKE ?,sex LIKE ?,birthday LIKE ?";
try{
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, "%"+keyWord+"%");
pstmt.setString(2, "%"+keyWord+"%");
pstmt.setString(3, "%"+keyWord+"%");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
User user=new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setSex(rs.getString(3));
user.setBirthday(rs.getDate(4));
all.add(user);
}
rs.close();
}
catch(Exception e){
throw e;
}
finally{
if(pstmt != null){
try{
pstmt.close();
}
catch(Exception e){
throw e;
}
}
conn.close();
}
return all;
}
//通过id,查询
public User findByID(int id) throws Exception{
User user=new User();
PreparedStatement pstmt=null;
String sql="SELECT id,name,sex,birthday FROM user WHERE id =?";
try{
pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setSex(rs.getString(3));
user.setBirthday(rs.getDate(4));
}
rs.close();
}
catch(Exception e){
throw e;
}
finally{
if(pstmt != null){
pstmt.close();
}
dbc.close();
}
return user;
}}
如果你确认jar包添加好了,打开那个jar包,看看里面有没有org.gjt.mm.mysql这个类
你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
那应该是org.gjt.mm.mysql.driver这么写吧
你好,是有org.gjt.mm.mysql这个包,里面有Driver.class
那应该是org.gjt.mm.mysql.driver这么写吧
你好,谢谢你,的确是你说的这样,我之前不用加Driver也行的,不过加了的确成功了,分就给你了,也谢谢其他有评论的人。