我使用 SQL Server 2008 作为数据库。并使用JDBC,我以 Under 的身份访问数据库。
public class DBConn {
public static void main(String[] args) throws ClassNotFoundException, SQLException{
Connection con =null;
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://BG4WS0552:1433;databaseName=DynamicsAx2009",
"TESTUSER", "Sonata@123");
Statement stmt = con.createStatement();
String query = "SELECT * FROM custtable";
DatabaseMetaData dmd = con.getMetaData();
ResultSet rs = dmd.getColumns ("DynamicsAx2009", "%", "custtable", null) ;
ResultSetMetaData rsmd = rs.getMetaData();
int i = 1;
while (rs.next()) {
System.out.print(i++ + " COLUMN_NAME is " + rs.getString("COLUMN_NAME"));
System.out.print(" TYPE:" + rs.getString("TYPE_NAME"));
System.out.println(" DATA_TYPE is " + rs.getString("DATA_TYPE"));
}
}}
参见上面的代码,我已经注释掉了 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")。即使我的程序运行良好。请在不注册驱动程序的情况下告诉我它是如何工作的。