关于转数据库的问题,急

旋转的转头 2008-02-26 09:47:15
package com.logistic.data;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.sql.Date;
//import java.text.SimpleDateFormat;

/**
****************************************************
*类名称: Db<br>
*类功能: 数据库操作 <br>
*创建: ckjava<br>
****************************************************
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DataConnect {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public static int error=0;

/***************************************************
*函数名称: getCon()<br>
*函数功能: 获取数据库连接<br>
*返回值: 无<br>
*参数说明: 无<br>
*创建: ckjava
****************************************************/
public static synchronized Connection getCon()throws Exception{
Context ctx;
DataSource ds;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/DBPool");
if(ds==null){
System.err.println();
System.err.println("数据连接打开+"+(++error));
}
return ds.getConnection();

}catch(SQLException e){
System.out.print(e);
throw e;
}
catch(NamingException e){
System.out.print(e);
throw e;
}
}
这个是连接MYSQL的bean,我想把它转换成连接sql server的bean,如下:
ackage com.logistic.data;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.sql.Date;
//import java.text.SimpleDateFormat;

/**
****************************************************
*类名称: Db<br>
*类功能: 数据库操作 <br>
*创建: ckjava<br>
****************************************************
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DataConnect {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
public static int error=0;

/***************************************************
*函数名称: getCon()<br>
*函数功能: 获取数据库连接<br>
*返回值: 无<br>
*参数说明: 无<br>
*创建: ckjava
****************************************************/
public static synchronized Connection getCon()throws Exception{

try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=logistic";
Connection con= DriverManager.getConnection(url,"sa","");
return con;
}catch(SQLException e){
System.out.print(e);
throw e;
}
catch(NamingException e){
System.out.print(e);
throw e;
}
}提示exception javax.naming.NamingException is nobody of corresponding try statement
应该怎么改才对
...全文
32 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧