1. 程式人生 > >解決Eclipse下匯入mysql驅動的問題

解決Eclipse下匯入mysql驅動的問題

先下載好資料庫驅動,然後拷貝到web工程下的WebContent->WEB-INF->lib目錄下,記得新增到java的庫檔案下是不可以的

然後再來測試一下

<body>
<%
	try{
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/mysql";
		String username = "root";
		String password = "123456";
		Connection conn = DriverManager.getConnection(url,username,password);
		
		if(conn != null){
			out.println("資料庫連線成功!");
			conn.close();
		}else{
			out.println("資料庫連線失敗!");
		}
	}catch(ClassNotFoundException e){
		e.printStackTrace();
	}catch(SQLException e){
		e.printStackTrace();
	}
%>
</body>

結果連線成功