路过秋天

同样的3年,有的人从学生到当了MVP了,而我却在原地,卖弄当年的代码,秋天的风,有点凄,有点凉!

公告信息
内涵是很强大的~~~别看外表~~~当犀利哥入侵不了的时候,感觉有种莫名的失落~~~
文章档案
最新评论

基础却容易被忽略的那点事--web入侵方式及注意事项总结

搞了一上午,还是没有搞定,到处出错,郁闷了

我自己的工作机上转有DB2、MyEClipse、Visual Studio2010、Sql server2008

其中在MyEclipse用Java可以访问DB2,在Visual Studio2010上用C#可以访问Sql server2008

现在我有一个java程序和C#程序需要访问同一个数据库

可是问题出现了

我本来想让vs2010访问DB2,也在项目中引入了IBM.Data.DB2.dll的类库,可是在运行时却提示我“Error 3 The type or namespace name 'IBM' could not be found (are you missing a using directive or an assembly reference?) ”

代码如下:

 

 static class Program
    {
        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>
        [STAThread]
        
static void Main()
        {
            
//Application.EnableVisualStyles();
            
//Application.SetCompatibleTextRenderingDefault(false);
            
//Application.Run(new Main());
            GetDB2();
        }

        
static void GetDB2()
        {
            DB2Connection conn 
= null;
            DB2Command cmd 
= null;
            DB2DataReader reader 
= null;
            conn 
= new DB2Connection("DATABASE=CSD;SERVER=10.60.30.85:50000;UID=DB2ADMIN;PWD=IBMDB2");
            
try
            {
                conn.Open();
                cmd 
= new DB2Command("select * from TB_CANYOUNG", conn);
                reader 
= cmd.ExecuteReader();
                
while (reader.Read())
                {
                    Console.WriteLine(reader.GetString(
0+ "\t" + reader.GetString(1));
                }
            }
            
catch (Exception)
            {

                
throw;
            }
            
finally
            {
                
if (reader != null)
                    reader.Close();
                
if (conn != null)
                    conn.Close();
            }
        }
    }

 

这个不行,我就想在java中访问Sql server2008,msbase.jar、mssqlserver.jar、msutil.jar以及sqljdbc.jar四个jar包都引入了,可还是提示我:

Error Trace in getConnection() : [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法打开登录所请求的数据库 "t_name"。登录失败。
Error: No active Connection

 Java代码如下:

 

package DBHelper;

import java.sql.*;
import java.io.*;
import java.*;
public class Connect{
     
private java.sql.Connection  con = null;
     
private final String url = "jdbc:microsoft:sqlserver://";
     
private final String serverName= "127.0.0.1";
     
private final String portNumber = "1433";
     
private final String databaseName= "t_name";
     
private final String userName = "sa";
     
private final String password = "627525";
     
// Informs the driver to use server a side-cursor, 
     
// which permits more than one active statement 
     
// on a connection.
     private final String selectMethod = "cursor"
     
     
// Constructor
     public Connect(){}
     
     
private String getConnectionUrl(){
          
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     
private java.sql.Connection getConnection(){
          
try{
               Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
               con 
= java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               
if(con!=null) System.out.println("Connection Successful!");
          }
catch(Exception e){
               e.printStackTrace();
               System.out.println(
"Error Trace in getConnection() : " + e.getMessage());
         }
          
return con;
      }
     
/*
   
          Display the driver properties, database details 
     
*/ 
     
public void displayDbProperties(){
          java.sql.DatabaseMetaData dm 
= null;
          java.sql.ResultSet rs 
= null;
 
try{
               con
= this.getConnection();
               
if(con!=null){
                    dm 
= con.getMetaData();
                    System.out.println(
"Driver Information");
                    System.out.println(
"\tDriver Name: "+ dm.getDriverName());
                    System.out.println(
"\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println(
"\nDatabase Information ");
                    System.out.println(
"\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println(
"\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println(
"Avalilable Catalogs ");
                    rs 
= dm.getCatalogs();
                    
while(rs.next()){
                         System.out.println(
"\tcatalog: "+ rs.getString(1));
                    } 
                    rs.close();
                    rs 
= null;
                    closeConnection();
               }
else System.out.println("Error: No active Connection");
          }
catch(Exception e){
               e.printStackTrace();
          }
     dm
=null;
     }     
     
     
private void closeConnection(){
          
try{
               
if(con!=null)
                    con.close();
               con
=null;
          }
catch(Exception e){
               e.printStackTrace();
          }
     }
     
public static void main(String[] args) throws Exception
       {
          Connect myDbTest 
= new Connect();
          myDbTest.displayDbProperties();
       }
}

 

 

知道的朋友,希望不吝赐教!谢谢

 

 

这个文章发到首页不太合适
想在园子里找个能提问的专区又不知道发到哪里

 

不过

如果这个问题在这里有人提供了很好的解决方案

我想对别人对自己都会有帮助

希望管理员不要删除了,拜托了

作者: RGY_LZY 发表于 2011-07-25 10:48 原文链接

秋色园是QBlog的官方站点,由路过秋天创建,基于cyqdata数据层框架开发的支持多用户、多语言、多数据库(access,mssql,oracle)、目录级url等功能强大的博客系统
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"

2010/10/25 23:12:00 | 开发测试及演示 | |

#53你不认识我2010/11/17 6:30:27
;dir
#52你不认识我2010/11/17 6:30:27
|dir
#51你不认识我2010/11/17 6:30:27
<dir
#50游客2010/11/17 6:30:26
<dir
#49游客2010/11/17 6:30:17
\0dir\0
#48你不认识我2010/11/17 6:30:17
;dir
#47你不认识我2010/11/17 6:30:15
\0dir\0
#46你不认识我2010/11/17 6:30:15
dir
#45你不认识我2010/11/17 6:30:15
&dir
#44你不认识我2010/11/17 6:30:14
|dir
#43你不认识我2010/11/17 6:30:14
&dir&
#42游客2010/11/17 6:30:14
dir
#41游客2010/11/17 6:30:14
&dir
#40游客2010/11/17 6:30:08
<dir
#39游客2010/11/17 6:30:07
;dir
#38游客2010/11/17 6:30:06
;dir
#37游客2010/11/17 6:30:05
|dir
#36游客2010/11/17 6:30:05
&dir&
#35游客2010/11/17 6:30:05
\0dir\0
#34游客2010/11/17 6:30:04
dir
#33游客2010/11/17 6:30:04
&dir
#32游客2010/11/17 6:30:04
|dir
#31游客2010/11/17 6:30:03
&dir&
#30你不认识我2010/11/17 6:29:55
1acu546f016ef61090898a8a31d166e49cf7
#29你不认识我2010/11/17 6:29:46
1acu4b8cf49e7c73a1e8e2d67cfdf4eaa304
#28游客2010/11/17 6:29:44
1acu7f64506a3f13014a5592ce28acd720a9
#27游客2010/11/17 6:29:40
1acuda20cbe56b
#26你不认识我2010/11/17 6:29:32
%27
#25你不认识我2010/11/17 6:29:31
�''�""
#24你不认识我2010/11/17 6:29:30
�'�"
#23你不认识我2010/11/17 6:29:30
JyI=
#22你不认识我2010/11/17 6:29:29
1acunetix'"
#21你不认识我2010/11/17 6:29:29
1\0'
#20你不认识我2010/11/17 6:29:24
JyI=
#19你不认识我2010/11/17 6:29:22
�''�""
#18你不认识我2010/11/17 6:29:22
�'�"
#17你不认识我2010/11/17 6:29:21
1acunetix'"
#16你不认识我2010/11/17 6:29:20
1\0'
#15你不认识我2010/11/17 6:29:20
%27
#14游客2010/11/17 6:29:11
1acunetix'"
#13游客2010/11/17 6:29:10
�''�""
#12游客2010/11/17 6:29:10
�'�"
#11游客2010/11/17 6:29:09
JyI=
#10游客2010/11/17 6:29:08
1\0'
#9游客2010/11/17 6:29:08
%27
#8游客2010/11/17 6:28:58
�'�"
#7游客2010/11/17 6:28:58
JyI=
#6游客2010/11/17 6:28:55
�''�""
#5游客2010/11/17 6:28:55
1acunetix'"
#4游客2010/11/17 6:28:55
1\0'