Connect Java to a MySQL database -
how connect mysql database in java?
drivermanager
old way of doing things. better way datasource
, either looking 1 app server container configured you:
context context = new initialcontext(); datasource datasource = (datasource) context.lookup("java:comp/env/jdbc/mydb");
or instantiating , configuring 1 database driver directly:
mysqldatasource datasource = new mysqldatasource(); datasource.setuser("scott"); datasource.setpassword("tiger"); datasource.setservername("mydbhost.example.org");
and obtain connections it, same above:
connection conn = datasource.getconnection(); statement stmt = conn.createstatement(); resultset rs = stmt.executequery("select id users"); ... rs.close(); stmt.close(); conn.close();
wiki
Comments
Post a Comment