java.sql.SQLException:[Microsoft][ODBC 驱动程序管理器]未发现数据源名称且未指定默认驱动程序。求指点
源码:ConnDB.javapackagecom.core;importjava.io.InputStream;importjava.sql.*;importjava.u...
源码:ConnDB.java
package com.core;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/*数据库连接类*/
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "/com/connDB.properties"; //指定资源文件保存的位置
private static Properties prop = new Properties();
private static String dbClassName ="com.mysql.jdbc.Driver";
private static String dbUrl =
"jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=12345&useUnicode=true";
public ConnDB(){
try {
InputStream in=getClass().getResourceAsStream(propFileName);
prop.load(in); //通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动
dbUrl = prop.getProperty("DB_URL",
"jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=12345&useUnicode=true");
}
catch (Exception e) {
e.printStackTrace(); //输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl);
}
catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl);
}
return conn;
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
* 功能:执行添加操作
*/
public boolean executeAdd(String sql) {
boolean ret = false;
try {
conn = getConnection(); //调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.execute(sql); //执行添加动作
ret = true;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
ret = false;
} finally {
this.close();
}
return ret;
}
获取的查询字符串:login
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 展开
package com.core;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/*数据库连接类*/
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "/com/connDB.properties"; //指定资源文件保存的位置
private static Properties prop = new Properties();
private static String dbClassName ="com.mysql.jdbc.Driver";
private static String dbUrl =
"jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=12345&useUnicode=true";
public ConnDB(){
try {
InputStream in=getClass().getResourceAsStream(propFileName);
prop.load(in); //通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动
dbUrl = prop.getProperty("DB_URL",
"jdbc:mysql://127.0.0.1:3306/db_librarysys?user=root&password=12345&useUnicode=true");
}
catch (Exception e) {
e.printStackTrace(); //输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl);
}
catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl);
}
return conn;
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
* 功能:执行添加操作
*/
public boolean executeAdd(String sql) {
boolean ret = false;
try {
conn = getConnection(); //调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.execute(sql); //执行添加动作
ret = true;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
ret = false;
} finally {
this.close();
}
return ret;
}
获取的查询字符串:login
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序 展开
3个回答
展开全部
导入链接mysql的jar包了吗?
追问
mysql-connector-java-5.0.4-bin.jar就是这个吧
追答
是的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
您好,这样的情况建议您可以升级到最新版本试一下,或是联系一下官网客服比较好。
追问
嗯嗯,谢谢!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |