当前位置:编程学习 > JAVA >>

数据库连接不上

报错:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
但是包已经导了, 而且是放在WebRoot/WEB-INF/lib下,怎么还是找不到包 --------------------编程问答-------------------- try...catch了没有,如果能有代码更能清楚你的问题 --------------------编程问答-------------------- public class ConnectionFactory {
private static String driver = null;
private static String url = null;
private static String user = null;
private static String pwd = null;
private static Properties config = new Properties();
//闈欐€佹柟娉曟垨浠g爜鍧椾腑涓嶈兘璁块棶闈為潤鎬佹垚鍛?
static{
ClassLoader loader = ConnectionFactory.class.getClassLoader();
InputStream in = loader.getResourceAsStream("daconfig.properties");
try {
//宸茬粡灏嗗睘鎬ф枃浠朵腑鐨勫唴瀹瑰瓨鍒癱onfig瀵硅薄閲岄潰
config.load(in);
driver = config.getProperty("driver");
url = config.getProperty("url");
user = config.getProperty("user");
pwd = config.getProperty("possword");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Connection getConnection(){
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
Connection con = ConnectionFactory.getConnection();
System.out.println(con);
}
} --------------------编程问答-------------------- 项目---右键---Properties---java build path---libraries 中看mysql的驱动包在不,如果不在再点击右侧的add jars 添加进来就可以了 --------------------编程问答-------------------- 在啊,两个包都存在,ojdbc14,mysql-connector-java-5.1.6-bin --------------------编程问答-------------------- 看连接数据库的语句对吗,sql2000和sql2005,2008的不一样。。。看你用的什么版本数据库,对应什么语句 --------------------编程问答--------------------
引用 5 楼  的回复:
看连接数据库的语句对吗,sql2000和sql2005,2008的不一样。。。看你用的什么版本数据库,对应什么语句

数据库:mysql 5.0.18
sql语句:select*from emp where id=? --------------------编程问答-------------------- 配置文件中的driver没有写错吧? --------------------编程问答--------------------
引用 7 楼  的回复:
配置文件中的driver没有写错吧?

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ems
user=root
pwd=1234 --------------------编程问答-------------------- 实话告诉你吧,你的driver写成了Driver --------------------编程问答-------------------- 这种问题最好自己先单步一下。 --------------------编程问答-------------------- 这个不知道行吗,你试试
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433
user=root
pwd=1234
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,