关于servlet连接数据库的问题!急!! 在eclipse里直接运行向数据库添加数据的java文件没有问题!可是,用servlet的方式,就报错找不到 com.mysql.jdbc.Driver 非常的苦恼,请高手帮忙,感激不尽 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 2种方式都是在同一个project里面的吗?如果不是的话,那就是第2个里面没有放Jar包 不是在同一个工程里包都导入了呀真是奇怪啊!!用servlet就显示java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 这个是servletpublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "214845"; String sql = "insert into table1 (username,password) values(?,?) "; Connection connection; try { Class.forName(driver); connection = DriverManager.getConnection(url, username,password); PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1,"request.getParameter('username')"); pstmt.setString(1,"request.getParameter('password')"); pstmt.executeUpdate(); pstmt.close(); connection.close(); } catch (ClassNotFoundException e)//捕获类未找到异常 { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }这个是带main方法的public static void main(String[] args) { String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/test"; String username="root"; String password="214845"; String sql="insert into table1 (username,password) values(?,?) "; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url,username,password); PreparedStatement pstnt = conn.prepareStatement(sql); pstnt.setString(1,"1fds"); pstnt.setString(2,"123456"); pstnt.executeUpdate(); pstnt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }真是奇怪啊!直接运行main方法就可以添加,用servlert就不行了。 在servlet中写类引用import com.mysql.jdbc.Driver;看有没有问题 肯定是没导入jar包的问题,lz再仔细检查一下。 你直接把mysql的jar包复制到给工程的lib文件夹下 你直接把mysql的jar包复制到给工程的lib文件夹下是“该工程” java.lang.ClassNotFoundException: com.mysql.jdbc.Driver貌似没有导入驱动包 mysql的jar包放到lib目录下面,应该就不会有问题啦 非常感谢各位!!!问题解决啦!就是没把mysql的jar包放到lib目录下!!!谢谢啦!!! ExtJs TriggerField组件的fieldLabel显示不出来 如何回复邮箱。 好多问题(初学者) Struts + JFreeChart中一个很难搞定的问题?请高手帮忙 谁有fckeditor.java的fckeditor2.3.zip这个包! JSF高级编程问题 怎么获取执行过的所有sql语句 springMVC+uploadify3.1 上传大文件时出现IO ERROR [问题点数:100分] 请看看我的这个TOMCAT配置问题 高分:如何将JSP程序发布到JRUN或者TOMCAT? 关于jstl的问题,急等! js函数调用问题
包都导入了呀
真是奇怪啊!!
用servlet就显示java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "214845";
String sql = "insert into table1 (username,password) values(?,?) ";
Connection connection;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username,password);
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1,"request.getParameter('username')");
pstmt.setString(1,"request.getParameter('password')");
pstmt.executeUpdate();
pstmt.close();
connection.close();
}
catch (ClassNotFoundException e)//捕获类未找到异常
{
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
这个是带main方法的
public static void main(String[] args) {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="214845";
String sql="insert into table1 (username,password) values(?,?) ";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement pstnt = conn.prepareStatement(sql);
pstnt.setString(1,"1fds");
pstnt.setString(2,"123456");
pstnt.executeUpdate();
pstnt.close();
conn.close();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
真是奇怪啊!直接运行main方法就可以添加,用servlert就不行了。
import com.mysql.jdbc.Driver;
看有没有问题
是“该工程”
貌似没有导入驱动包
问题解决啦!
就是没把mysql的jar包放到lib目录下!!!谢谢啦!!!