// 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