纯Java代码用建立ODBC数据源的方法连接SQLServer2008,连接失败提示信息“sun.jdbc.odbc.JdbcOdbcDriver 20
数据源已经建立并配置好了,并且测试成功。运行Java程序后,提示信息是“数据库加载失败sun.jdbc.odbc.JdbcOdbcDriver”Java代码如下:pack...
数据源已经建立并配置好了,并且测试成功。运行Java程序后,提示信息是
“数据库加载失败sun.jdbc.odbc.JdbcOdbcDriver”
Java代码如下:
package shujuku;
import java.sql.*;
public class JDBConnection {
private final String url="jdbc:odbc:sqlconnet";
private final String userName="sa";
private final String password="741852";
private Connection con=null;
//加载驱动
static {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception ex){
System.out.println("数据库加载失败"+ex.getMessage());
}
}
//链接数据库
public boolean creatConnection(){
try{
con=DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
// System.out.println("连接成功");
}
catch(SQLException e){
System.out.println(e.getMessage());
System.out.println("creatConnectError!");
}
return true;
}
} 展开
“数据库加载失败sun.jdbc.odbc.JdbcOdbcDriver”
Java代码如下:
package shujuku;
import java.sql.*;
public class JDBConnection {
private final String url="jdbc:odbc:sqlconnet";
private final String userName="sa";
private final String password="741852";
private Connection con=null;
//加载驱动
static {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception ex){
System.out.println("数据库加载失败"+ex.getMessage());
}
}
//链接数据库
public boolean creatConnection(){
try{
con=DriverManager.getConnection(url,userName,password);
con.setAutoCommit(true);
// System.out.println("连接成功");
}
catch(SQLException e){
System.out.println(e.getMessage());
System.out.println("creatConnectError!");
}
return true;
}
} 展开
1个回答
展开全部
package com.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
public class DbHelper {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost;databasename=db_Blog";
private static final String UID="sa";
private static final String PWD="sasa";
public static Connection getConnection(){
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL,UID,PWD);
System.out.println("连接数据库成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("sb");
}
return con;
}
public static void main(String []p){
getConnection();
}
public static Result runSelectSql(String sql){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static Result runSelectSql(String sql,Object[] params){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static boolean runInset(String sql,Object[] params){
boolean flag=false;
Connection con=null;
PreparedStatement pst=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
int a=pst.executeUpdate();
if(a>0){
flag=true;
}
else{flag=false;}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
}
这是以前我们课上写的 你看看
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
public class DbHelper {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost;databasename=db_Blog";
private static final String UID="sa";
private static final String PWD="sasa";
public static Connection getConnection(){
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL,UID,PWD);
System.out.println("连接数据库成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("sb");
}
return con;
}
public static void main(String []p){
getConnection();
}
public static Result runSelectSql(String sql){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static Result runSelectSql(String sql,Object[] params){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static boolean runInset(String sql,Object[] params){
boolean flag=false;
Connection con=null;
PreparedStatement pst=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
pst.setObject(i+1,params[i]);
}
int a=pst.executeUpdate();
if(a>0){
flag=true;
}
else{flag=false;}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
}
这是以前我们课上写的 你看看
更多追问追答
追问
我用的不是Microsoft提供的JDBC驱动,而是sun公司的ODBC数据源建立的连接,
追答
这样啊,这我倒是没接触过了,不过只要是链接驱动总没多大区别的,你们老师没给你们样板事例么,或者你百度下,应该可以的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询