求代码
------解决方案--------------------
- Java code
import java.sql.*;public class OracleDB { public static Connection getConnection() { Connection conn = null; try { String driver = "oracle.jdbc.OracleDriver"; String dbName = "DB_Name"; String dbURL = "jdbc:oracle:thin:@127.0.0.1:1521:"; String username = "username"; String password = "password"; Class.forName(driver).newInstance(); conn = DriverManager.getConnection(dbURL + dbName, username, password); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void closeConnection(Connection conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }}
------解决方案--------------------
- Java code
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class TestJDBC { /** * @param args */ /**jdbc:sqlserver://localhost:1433;databaseName=bbsDB;user=Lenovo;password=123456" * @param args */ public static void main(String[] args) { Connection con=null; Statement stmt=null; ResultSet rs=null; try{ //jdbc:sqlserver://localhost:1433;databaseName=bbsDB;user=Lenovo;password=123456 //jdbc:sqlserver://localhost;user=MyUserName;password=*****; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con=DriverManager.getConnection("jdbc:sqlserver://localhost;user=Lenovo;password=123456"); stmt=con.createStatement(); rs=stmt.executeQuery("Select * from bbsUsers "); while(rs.next()){ //得到内容 } }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } finally{ try{ if(con!=null){ con.close(); con=null; } if(stmt!=null){ stmt.close(); stmt=null; } if(rs!=null){ rs.close(); rs=null; } }catch(SQLException e){ e.printStackTrace(); } } }}把连接字符串改成oracle的连接字符串就可以了,我这个是SQL Server连接字符串