首页 > 代码库 > JAVA-数据库连接【转】

JAVA-数据库连接【转】

 

SqlServer、Oracle、MySQL的连接,除了地址和驱动包不同,其他都一样的。

 1     public String urlString="jdbc:sqlserver://localhost:1433;databaseName=crebas;"; 2         //"jdbc:mysql://localhost:3306/crebas?autoReconnect=true&useUnicode=true&characterEncoding=utf8" 3         //"jdbc:oracle:thin:@localhost:1521:ORCL" 4     public String nameString="sa"; 5     public String password="123"; 6     public void test() 7     { 8         String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 9             //"com.mysql.jdbc.Driver"10             //"oracle.jdbc.driver.OracleDriver"11         try12         {13             Class.forName(driver);14             Connection conn = DriverManager.getConnection(urlString, nameString, password);15             PreparedStatement pstmt = conn.prepareStatement("Select * from T_Admin");16             ResultSet rs = pstmt.executeQuery();17             rs.next();18             System.out.println(rs.getString(1));19             System.out.println(rs.getString(2));20             System.out.println(rs.getString(3));21         } catch (Exception e)22         {23             e.printStackTrace();24         }25     }

最基础的连接方式,好久之前整理的了,备忘一下。