在java中怎样与SQL2000做连接??
老师给我们留了SQL2000的课程设计~~
我想用JAVA与之结合可是在学习时
我在学习java与SQL2000时不知道这样与之连接~~~
请大家给一点点意见~~~和学习的经验之谈~~
问题点数:0、回复次数:2Top
1 楼newmeteor(圆缘)回复于 2005-06-04 14:34:37 得分 0
将安装驱动后产生的三个文件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, 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语句,你就可以运行了!
Top
2 楼newmeteor(圆缘)回复于 2005-06-04 14:36:05 得分 0
你先查一下以回答的贴子!这个问题很多人都问过了!
我也已经回复,5遍以上了!Top




