2
回答

我改的一个基于struts 2的信息管理系统在连接oracle数据库时出现问题,系统原来用的是SQLserver数据库。我修改了一些链接数据库的类。原来链接SQLserver的链接代码和我改的链接代码都贴上,希望高手能帮助解决。
报错:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ConnectionDatabase { // 加载驱动 private final String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // 连接数据库的路径 private final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;databasename=hospitaldb"; // 连接数据库的用户名 private final String DBUSER = "sa"; // 连接数据库的密码 private final String DBPASSWORD = "sa"; // 连接数据库的对象 public Connection conn = null; private PreparedStatement ps = null; private ResultSet rs = null; public ConnectionDatabase() { try { Class.forName(DBDRIVER); this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); } catch (Exception e) { e.printStackTrace(); System.out.println("数据库连接失败" + this.conn + DBURL + DBUSER + DBPASSWORD); } } // 取得数据库连接 public Connection getConnection() { return this.conn; } // 关闭数数据库连接 public void close() { try { this.conn.close(); } catch (Exception e) { } } // 实现查询 public ResultSet select(String sql) { try { ps = getConnection().prepareStatement(sql); rs = ps.executeQuery(); return rs; } catch (SQLException se) { se.printStackTrace(); return null; } } // 实现增删改 public boolean noselect(String sql) { try { ps = getConnection().prepareStatement(sql); int i = 0; i = ps.executeUpdate(); return i >= 1; } catch (SQLException se) { se.printStackTrace(); return false; } finally { } } // 将于数据库相连的各个对象顺序关闭 public void closeAll(ResultSet r, Statement s, Connection c) { try { if (r != null) r.close(); if (s != null) s.close(); if (s != null) s.close(); if (c != null) c.close(); } catch (SQLException e) { e.printStackTrace(); } } }
import java.sql.*; public class ConnectionDatabase { //dbUrl数据库连接串信息,其中“1521”为端口,“ora9”为sid String dbUrl = "jdbc:oracle:thin:@211.87.20.15:1521:ora9"; //theUser为数据库用户名 String theUser = "admin"; //thePw为数据库密码 String thePw = "1234"; //几个数据库变量 Connection conn = null; Statement ps; ResultSet rs = null; //初始化连接 public ConnectionDatabase() { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); //与url指定的数据源建立连接 conn = DriverManager.getConnection(dbUrl, theUser, thePw); //采用Statement进行查询 ps = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } //执行查询 public ResultSet executeQuery(String sql) { rs = null; try { rs = ps.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public void close() { try { ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ResultSet newrs; ConnectionDatabase newjdbc = new ConnectionDatabase(); newrs = newjdbc.executeQuery("select * from eventtype"); try { while (newrs.next()) { System.out.print(newrs.getString("event_type")); System.out.println(":"+newrs.getString("content")); } } catch (Exception e) { e.printStackTrace(); } newjdbc.close(); } }