在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
搞了一上午,还是没有搞定,到处出错,郁闷了 我自己的工作机上转有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"。登录失败。 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(); } }
知道的朋友,希望不吝赐教!谢谢
这个文章发到首页不太合适
不过 如果这个问题在这里有人提供了很好的解决方案 我想对别人对自己都会有帮助 希望管理员不要删除了,拜托了 |
请发表评论