当前位置: 技术问答>java相关
一段小代码!急!在线等待!分不多望原谅!
来源: 互联网 发布时间:2015-11-19
本文导语: package second; import java.net.URL; import java.sql.*; import java.util.*; class JdbcOdbc_test { ResultSet results; ResultSetMetaData rsmd; DatabaseMetaData dma; Connection con; int numCols, i; public static void main(String a[]){ Jdbc...
package second;
import java.net.URL;
import java.sql.*;
import java.util.*;
class JdbcOdbc_test
{
ResultSet results;
ResultSetMetaData rsmd;
DatabaseMetaData dma;
Connection con;
int numCols, i;
public static void main(String a[]){
JdbcOdbc_test odbc=new JdbcOdbc_test();
System.out.print(odbc.numCols);
}
public JdbcOdbc_test()
{
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";
String query = "SELECT DISTINCTROW FoodName FROM Food "+ "WHERE (FoodName like 'C%');";
try
{
//加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver ");
//连接数据库
con = DriverManager.getConnection(url);
//获取数据库的元数据
dma =con.getMetaData();
System.out.println("Connected to:"+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
//将数据库中的表的名称转储出来
String[] types = new String[1];
types[0] = "TABLES";
//请注意通配符是 % 符号(而不是“*”)
results = dma.getTables(null, null, "%", types);
results.close();
}
catch (Exception e)
{System.out.println(e);}
//获取表列的名称
System.out.println("--Column Names--");
try {
results =dma.getColumns(null, null, "name", null);
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
while (results.next() )
{
String cname = results.getString("COLUMN_NAME");
System.out.print(cname + " ");
System.out.println();
}
results.close();
}
catch (Exception e)
{System.out.println(e);}
//列出一个列的内容 -- 这是一个查询
try {
Statement stmt = con.createStatement();
results =
stmt.executeQuery("SELECT * FROM sysobjects;");
}
catch (Exception e)
{System.out.println("query exception");}
//尝试实际的 SQL 语句
try
{
Statement stmt = con.createStatement();
results = stmt.executeQuery(query);
}
catch (Exception e)
{
System.out.println("query exception");
}
}
}
程序运行之后:
java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
--Column Names--
java.lang.NullPointerException
query exception
query exception
0
SQLServerDriver的 classpath 以加,master数据库连接成功!为什么会这样呢?请高手指点!?
import java.net.URL;
import java.sql.*;
import java.util.*;
class JdbcOdbc_test
{
ResultSet results;
ResultSetMetaData rsmd;
DatabaseMetaData dma;
Connection con;
int numCols, i;
public static void main(String a[]){
JdbcOdbc_test odbc=new JdbcOdbc_test();
System.out.print(odbc.numCols);
}
public JdbcOdbc_test()
{
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";
String query = "SELECT DISTINCTROW FoodName FROM Food "+ "WHERE (FoodName like 'C%');";
try
{
//加载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver ");
//连接数据库
con = DriverManager.getConnection(url);
//获取数据库的元数据
dma =con.getMetaData();
System.out.println("Connected to:"+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
//将数据库中的表的名称转储出来
String[] types = new String[1];
types[0] = "TABLES";
//请注意通配符是 % 符号(而不是“*”)
results = dma.getTables(null, null, "%", types);
results.close();
}
catch (Exception e)
{System.out.println(e);}
//获取表列的名称
System.out.println("--Column Names--");
try {
results =dma.getColumns(null, null, "name", null);
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
while (results.next() )
{
String cname = results.getString("COLUMN_NAME");
System.out.print(cname + " ");
System.out.println();
}
results.close();
}
catch (Exception e)
{System.out.println(e);}
//列出一个列的内容 -- 这是一个查询
try {
Statement stmt = con.createStatement();
results =
stmt.executeQuery("SELECT * FROM sysobjects;");
}
catch (Exception e)
{System.out.println("query exception");}
//尝试实际的 SQL 语句
try
{
Statement stmt = con.createStatement();
results = stmt.executeQuery(query);
}
catch (Exception e)
{
System.out.println("query exception");
}
}
}
程序运行之后:
java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
--Column Names--
java.lang.NullPointerException
query exception
query exception
0
SQLServerDriver的 classpath 以加,master数据库连接成功!为什么会这样呢?请高手指点!?
|
你用jb7的话要这么配置:
1。菜单的tools->configure libraries->点击new按钮->点击add按钮(把你用到的jar加进去,配置一个名字,比如sqlserverA)
2。菜单project->project proerties->paths(页面)->requird libraries->add没创建的sqlserverA,然后编译运行了。
1。菜单的tools->configure libraries->点击new按钮->点击add按钮(把你用到的jar加进去,配置一个名字,比如sqlserverA)
2。菜单project->project proerties->paths(页面)->requird libraries->add没创建的sqlserverA,然后编译运行了。
|
另外告诉你一个不好的消息,在jb6里面不能运行带jdbc的类,这是jb的bug.
运行就会告诉你驱动程序找不到,我测试过,但是驱动程序已经田加进去了,在jb5里面是可以的。好象jb7也是这样,听同事说的。但是我没有实验。
运行就会告诉你驱动程序找不到,我测试过,但是驱动程序已经田加进去了,在jb5里面是可以的。好象jb7也是这样,听同事说的。但是我没有实验。
|
你试试
catch(SQLException ex)
{System.out.println("n******n");
while (ex!=null)
{System.out.println("SQLState:"+ ex.getSQLState());
System.out.println("Message:"+ ex.getMessage());
System.out.println("Vendor:"+ ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(SQLException ex)
{System.out.println("n******n");
while (ex!=null)
{System.out.println("SQLState:"+ ex.getSQLState());
System.out.println("Message:"+ ex.getMessage());
System.out.println("Vendor:"+ ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}