用java程序连接sql数据库,错误!

来源:百度知道 编辑:UC知道 时间:2024/05/18 01:39:54
怎么连接sql数据库?比如:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Url = "jdbc:odbc:CHAT_DSN“; connection=DriverManager.getConnection(Url,"ChatUser","ChatUser");
//JOptionPane.showMessageDialog(null,connection.toString());
stm=connection.createStatement();

sql2000需要怎么设置?2个ChatUser是什么?需要设置数据源吗?怎么弄?比如我需要连接chat数据库里的users表!
我运行时候一直提示:Could not open port: 888!
我是xp系统,装不了sp4,报错就是Could not open port: 888!

你连接字符串缺少ip,端口,库名
--odbc桥连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
--DriverManager.getConnection(连接字符串, 登录用户名, 登录密码);
例:
String Url = dbc:microsoft:sqlserver://localhost:1433; DatabaseName=chat";
--localhost数据库在本机,要不就写IP地址,DatabaseName=chat,指连接chat库
DriverManager.getConnection(Url,"ChatUser","ChatUser");

--驱动连接
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
--DriverManager.getConnection(连接字符串, 登录用户名, 登录密码);
例:
String Url = dbc:microsoft:sqlserver://localhost:1433; DatabaseName=chat"
DriverManager.getConnection(Url,"ChatUser","ChatUser");

---不好意思,驱动连接我写错了,没有注意到
String Url = “jdbc:ODBC:数据库名“
这就你和开始的一样了

看看你报什么错误,一般现在都还要打个SP2或者SP4的补丁,不知道你打了没有,没有一般都会报错

你是在使用odbc连接吗?还是使用jdbc,如果是jdbc,你的url有问题

我原来写的小例子,可以拿来直接用,需要改动的地方有注释

Connection databaseConnect = null; // 数据库连接
St