java连接oracle以及Mysql
java连接oracle
View Code
1 import java.sql.*; 2 public class Conn { 3 4 public static void main(String[] args) { 5 6 Statement stem = null; 7 ResultSet st = null; 8 Connection ct = null; 9 try { 10 Class.forName("oracle.jdbc.driver.OracleDriver"); 11 String url="jdbc:oracle:thin:@localhost:1521:ORCL"; 12 String UserName="scott"; 13 String passWord="m1234"; 14 try { 15 ct=DriverManager.getConnection(url,UserName,passWord); 16 stem=ct.createStatement(); 17 st=stem.executeQuery("select empno,ename from emp"); 18 while(st.next()){ 19 System.out.println(st.getString("empno")+","+st.getString("ename")); 20 } 21 22 } catch (SQLException e) { 23 24 e.printStackTrace(); 25 } 26 27 28 } catch (ClassNotFoundException e) { 29 30 e.printStackTrace(); 31 } 32 33 } 34 }
java连接mysql
View Code
1 Connection conn = null; 2 Statement stem = null; 3 ResultSet rs = null; 4 try{ 5 String url = "jdbc:mysql://127.0.0.1:3306/users"; 6 String user = "root"; 7 String uPwd = "123456"; 8 try{ 9 //加载驱动 10 Class.forName("com.mysql.jdbc.Driver"); 11 //连接数据库 12 conn = DriverManager.getConnection(url,user,uPwd); 13 stem = conn.createStatement(); 14 rs = stem.executeQuery("select * from users"); 15 16 while(rs.next()){ 17 18 System.out.println(rs.getString("uName") + rs.getString("pwd") + rs.getString("id")); 19 20 } 21 }catch (Exception e) { 22 e.printStackTrace(); 23 } 24 }catch(Exception ex){ 25 ex.printStackTrace(); 26 } 27 } 28 }