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

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -