Official Site

click here to view our new site launched

Pages

Tuesday, April 19, 2011

MYSQL connectivity Java

// java bean for database connection to MySQL

package db;
import java.sql.*;

public class DbConnect {

private Connection conn = null;

public Connection OracleConnect() {
try {

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println("connected");
//return conn;
} catch (Exception e) {
//in case no password error catching error and try again.
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
}
catch (Exception ee) {
System.out.println("Error in Database Connection2"+ee.getMessage());
}

System.out.println("Error in Database Connection1"+e.getMessage());
}
finally{
return (conn);
}
}

protected void CloseConnection() {
try {
conn.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}



No comments:

Post a Comment

$2.49 .COM at GoDaddy.com! Expires 5/14/13