Sunday, 15 February 2015

Connect web java to database system using MySQL -



Connect web java to database system using MySQL -

i've created java web login info account(username, password). want connect database using mysql ver5.6. have database id, username, password.

package dao; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; public abstract class dbconnectiondao { /** connection */ protected connection connection; /** classname */ private string classname = "com.mysql.jdbc.driver"; /** stringconnection */ private string stringconnection = "jdbc:mysql://localhost/test?user=root&password=abc123&characterencoding=utf-8"; /** * @constructor phuong thuc khoi tao */ public dbconnectiondao() { seek { class.forname(classname); // open connection connection = drivermanager.getconnection(stringconnection); } grab (classnotfoundexception e) { system.err.println("class not found! please review library"); } grab (sqlexception e) { e.printstacktrace(); system.err.println("loi truy van"); } }

}

public class database {

connection conn = null; int id; static{ seek { class.forname("com.mysql.jdbc.driver"); system.out.println("driver loaded successfully.."); } grab (classnotfoundexception e) { e.printstacktrace(); } } public connection connect(){ seek { conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/databasename", "root", "root"); if(conn!=null) { system.out.println("database connected successfully..."); } } grab (sqlexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming conn; }

java mysql database logging web

No comments:

Post a Comment