求助:java连接SQLServer 想连接SQLSERVER7.0,用的是JTDS的驱动,也设了环境变量,仍然提示classnotfound,不知怎么回事,哪位达人能指点迷津.最好是从下驱动到设变量都有的,越详细越好,不胜感激~~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /* * put your module comment here * formatted with JxBeauty (c) [email protected] */import java.sql.*;/** * put your documentation comment here */public class Connect { /** * put your documentation comment here * @param args * @exception Exception */ public static void main (String[] args) throws Exception { String user = "sa"; String pwd = "sa"; String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;selectMethod=cursor"; String drv = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; try { Class.forName(drv).newInstance(); Connection con = DriverManager.getConnection(url, user, pwd); Statement stmt = con.createStatement(); String sql = "select * from jobs "; ResultSet result = stmt.executeQuery(sql); while (result.next()) { System.out.print(result.getString(1)); System.out.print(result.getString(2)); System.out.print(result.getString(3)); System.out.println(result.getString(4)); } stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }}不行 换JSQLConnect驱动试试 一楼那位朋友的方法我试过,不行.可能我的环境变量什么的设的不对,能否将步骤说的详细些。我用的是JTDS的驱动,先下了个jtds-0.6.jar文件拷到JDK安装路径下的LIB目录下。在CALSSPATH里设置变量为JDK安装路径\lib\jtds-0.6.jar。但仍然提示classnotfound 需要Class.forName(drv).newInstance();吗?我一般只写:Class.forName(drv);就行了。 将安装驱动后产生的三个文件msbase.jar;mssqlserver.jar;msutil.jar放到C:\j2sdk1.4.2_05\jre\lib\ext目录下就可以编程了,不需要另外配置环境变量了!然后试着运行下面的代码! import java.sql.*; import java.io.*; import java.util.*; /** 6. This program tests that the database and the JDBC 7. driver are correctly configured. */ class TestDB { public static void main (String args[]) { try { runTest(); } catch (SQLException ex) { while (ex != null) { ex.printStackTrace(); ex = ex.getNextException(); } } catch (IOException ex) { ex.printStackTrace(); } } /**32. Runs a test by creating a table, adding a value, showing the table contents, and33. removing the table.34. */ public static void runTest() throws SQLException, IOException { Connection conn = getConnection(); try { Statement stat = conn.createStatement(); ResultSet result = stat.executeQuery("SELECT * FROM Mst_Company"); while(result.next()) { System.out.println(result.getString(1)); } } finally { conn.close(); } } /** Gets a connection from the properties specified in the file database.properties @return the database connection */ public static Connection getConnection() throws SQLException, IOException { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); } catch(Exception e) { System.out.println(e); } String url ="jdbc:microsoft:sqlserver://192.168.1.24:1433;DatabaseName=marunaka"; String username = "dbuser"; String password = "qaz123"; return DriverManager.getConnection(url, username, password); } }修改一下,ip地址,数据库名,用户名,密码,sql语句,你就可以运行了! 请给个microsoft SQLServer7.0驱动下载的连接,谢谢! 不好意思,今天上午这里不能登录。邮箱是[email protected],谢谢! 没收到哎,:(再发一次,好吧?那个驱动有多大?小于1.5M的话,可发到[email protected] 下了一个I-NET的驱动,已经搞好了.谢谢各位!特别感谢圆缘:) JAVA将CLOB类型的照片文件以图片方式写入到本地 马上就要找工作了,求几本JAVA面试教材? 现在选3G方向的软件开发怎么样? 是这方面的朋友给点意见啊! 调试程序真的是很难! jnbridge,JAVA里的方法如何在VS2005里调用,请指教 jre1.5.0_02与jre1.6.0_07可以删掉一个吗? 请问我的JAVA程序乍了 在线求救!!!!!很容易的问题。进来看看了。 jbuilder的配置问题----初学者请求帮助 到那里能找到SUN ONE 的中文资料? 如何往一个jar包里面添加文件 200 分求教,估计有人知道吧,请教各位,先行谢过!!!!!!!!!!!!
* put your module comment here
* formatted with JxBeauty (c) [email protected]
*/
import java.sql.*;
/**
* put your documentation comment here
*/
public class Connect { /**
* put your documentation comment here
* @param args
* @exception Exception
*/
public static void main (String[] args) throws Exception {
String user = "sa";
String pwd = "sa";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;selectMethod=cursor";
String drv = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
try {
Class.forName(drv).newInstance();
Connection con = DriverManager.getConnection(url, user, pwd);
Statement stmt = con.createStatement();
String sql = "select * from jobs ";
ResultSet result = stmt.executeQuery(sql);
while (result.next()) {
System.out.print(result.getString(1));
System.out.print(result.getString(2));
System.out.print(result.getString(3));
System.out.println(result.getString(4));
}
stmt.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}不行 换JSQLConnect驱动试试
Class.forName(drv).newInstance();吗?
我一般只写:
Class.forName(drv);就行了。
放到C:\j2sdk1.4.2_05\jre\lib\ext目录下就可以编程了,不需要另外配置环境变量了!然后试着运行下面的代码!
import java.sql.*;
import java.io.*;
import java.util.*;
/**
6. This program tests that the database and the JDBC
7. driver are correctly configured.
*/
class TestDB
{
public static void main (String args[])
{
try
{
runTest();
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
} /**
32. Runs a test by creating a table, adding a value, showing the table contents, and
33. removing the table.
34. */
public static void runTest()
throws SQLException, IOException
{
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement(); ResultSet result = stat.executeQuery("SELECT * FROM Mst_Company");
while(result.next())
{
System.out.println(result.getString(1));
}
}
finally
{
conn.close();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
}
catch(Exception e)
{
System.out.println(e);
} String url ="jdbc:microsoft:sqlserver://192.168.1.24:1433;DatabaseName=marunaka";
String username = "dbuser";
String password = "qaz123"; return DriverManager.getConnection(url, username, password);
}
}修改一下,ip地址,数据库名,用户名,密码,sql语句,你就可以运行了!
那个驱动有多大?小于1.5M的话,可发到[email protected]