`

mysql sql server 2000/2008 oracle10g数据库连接代码

 
阅读更多

这些代码都经过测试,没什么问题

1.连接MySql数据库

 

public class TestMySqlConnection {
	public static void main(String[] srg) throws SQLException {
		String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动
		String dbURL = "jdbc:mysql://localhost:3308/test"; // 连接服务器和数据库sample
		String userName = "root"; // 默认用户名
		String userPwd = "你的密码"; // 密码
		Connection dbConn = null;

		try {
			Class.forName(driverName);
			dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
			System.out.println("MySQL数据库连接成功!"); // 如果连接成功控制台输出Connection
															// Successful!
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (dbConn != null) {
				dbConn.close();
			}
		}
	}
}
 

 

 

2.连接Sql Server 2000数据库

 

public class TestSqlServer2000Connection{ 
    
   
 
    public static void main(String args[]) throws SQLException{    
    	    Connection con = null;
    	 try{ 
             String name="com.microsoft.jdbc.sqlserver.SQLServerDriver";
             String url="jdbc:microsoft:sqlserver://localhost:1433";  
             Class.forName(name);
             String  user = "sa";
             String  password = "你的密码";
             con=DriverManager.getConnection(url,user,password);
             System.out.println("SQL Server 2000数据库连接成功!");
             con.close();           
       }catch(Exception e){
             e.printStackTrace();
       }
       finally{
    	   if (con != null) {
			con.close();
		}
       }
    }
 
} 
 

 


3.连接Sql Server 2008数据库

 

public class TestSqlServer2008Connection {
	public static void main(String[] args) throws SQLException {
		Connection conn = null;
		try {
			String url="jdbc:sqlserver://localhost:1433;databaseName=msdb";
			String user="sa";
			String password="你的密码";
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn= DriverManager.getConnection(url,user,password);
			if (conn != null) {
				System.out.println("SQL Server 2008 数据库连接成功");
			}
		}

		// Handle any errors that may have occurred.
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if (conn != null) {
				conn.close();
			}
		}

		
	}
}
 

 


4.连接Oracle 10g数据库

 

public class TestOracle10gConnection {
	  public static void main(String[] args) throws Exception {
		  try{
	            String driver = "oracle.jdbc.driver.OracleDriver";
	            Class.forName(driver); 
	            String url="jdbc:oracle:thin:@localhost:1521:orcl";
	            java.util.Properties info = new java.util.Properties();
	            info.put ("user", "sys");
	            info.put ("password", "你的密码");
	            info.put ("internal_logon","sysdba");
	            Connection conn = DriverManager.getConnection(url,info); 
	            System.out.println("Oracle 10g数据库 连接成功");
	            conn.close();
		  }
		  catch(Exception e){
              e.printStackTrace();
          }            
 }
}

 

 或者

 

public static void main(String[] args) {
        String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
        String JDBC_STRING = "jdbc:oracle:thin:@127.0.01:1521:orcl"; 
        String USER_NAME = "SYSTEM";
        String PASSWD = "你的密码";
        Connection conn = null;
        try{
                Class.forName(JDBC_DRIVER);
                conn = DriverManager.getConnection(JDBC_STRING, USER_NAME, PASSWD);
                if (conn != null) {
					System.out.println("Oralce 10g 数据库连接成功");
				}
        }catch(SQLException sqlEx){
                sqlEx.printStackTrace();
        } catch (ClassNotFoundException e) {
                e.printStackTrace();
        } finally{
                try {
                        if(conn!=null) conn.close();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
        }
}
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics