按照如下VB代码,怎么写JAVA连接数据源的JDBC代码,谢谢
按照如下代码,我想用java连接数据库以便获取数据,因为下面代码好像是从数据源里获取数据的,所以我想java要用桥接方式获取数据,但写了java代码,但是没成功
VB代码
Set objConnection = CreateObject("ADODB.CONNECTION")
strConnectionString = "DSN=AR System ODBC Data Source;ARServer=globalar.jci.com;UID=ayuang;PWD=Jame12$;ARAuthentication=;ARUseUnderscores=1;SERVER=NotTheServer"
objConnection.Open strConnectionString
If (objConnection.State = 1) Then
MsgBox "数据库连接成功!"
End If
If (objConnection.State = 0) Then
MsgBox "连接数据库失败!"
End If
i = 0
str_Array_QueryResult = Array() '重新初始化数组为一个空数组
Set rs = CreateObject("ADODB.RECORDSET") '4 - 建立RECORDSET对象实例
Set objCommand = CreateObject("ADODB.COMMAND") '5 - 建立COMMAND对象实例
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT "&chr(34)&"HPD:HelpDesk"&chr(34)&"."&chr(34)&"Case ID+"&chr(34)&" from "&chr(34)&"HPD:HelpDesk"&chr(34)&" "&chr(34)&"HPD:HelpDesk"&chr(34)&" WHERE ("&chr(34)&"HPD:HelpDesk"&chr(34)&"."&chr(34)&"Assigned To Group+"&chr(34)&" Like "&chr(39)&"%apac.gen.as%"&chr(39)&") OR ("&chr(34)&"HPD:HelpDesk"&chr(34)&"."&chr(34)&"Entry Group"&chr(34)&" Like "&chr(39)&"%apac.gen.as%"&chr(39)&") ORDER BY "&chr(34)&"HPD:HelpDesk"&chr(34)&"."&chr(34)&"Case ID+"&chr(34)&""
rs.CursorLocation = 3
rs.Open objCommand
JAVA代码
String dbur1 = "jdbc:odbc:AR System ODBC Data Source";
conn = DriverManager.getConnection(dbur1,"ayuang","Jame12$");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from HPD:HelpDesk");
请问上面JAVA代码哪里写错了,请指教,还有就是按照VB代码,数据库名是什么?
[解决办法]
Class.forName("驱动名");String dbur1 = "jdbc:odbc:AR System ODBC Data Source";
conn = DriverManager.getConnection(dbur1,"ayuang","Jame12$");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from HPD:HelpDesk");
[解决办法]
这是什么?
String dbur1 = "jdbc:odbc:AR System ODBC Data Source";
连接字符串不对,具体数据库有具体的连接字符串,网上搜一个吧。。。
[解决办法]
*/
public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {
/* 如果rs不空,关闭rs */
if(rs != null){
try { rs.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果pstmt不空,关闭pstmt */
if(pstmt != null){
try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果conn不空,关闭conn */
if(conn != null){
try { conn.close();} catch (SQLException e) {e.printStackTrace();}
}
}
/**
* 执行SQL语句,可以进行增、删、改的操作,不能执行查询
* @param sql 预编译的 SQL 语句
* @param param 预编译的 SQL 语句中的‘?’参数的字符串数组
* @return 影响的条数
*/
public int executeSQL(String preparedSql,String[] param) {
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
/* 处理SQL,执行SQL */
try {
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
if( param != null ) {
for( int i = 0; i < param.length; i++ ) {
pstmt.setString(i+1, param[i]); // 为预编译sql设置参数
}
}
num = pstmt.executeUpdate(); // 执行SQL语句
} catch (ClassNotFoundException e) {
e.printStackTrace(); // 处理ClassNotFoundException异常
} catch (SQLException e) {
e.printStackTrace(); // 处理SQLException异常
} finally {
closeAll(conn,pstmt,null); // 释放资源
}
return num;
}
}