MySQL access from Hadoop MapReduce - java

I want to access MySQL database within my MapReduce program. I have a DBConnection class and I getConnection within the mapper class.The db.properties file is in place and with correct path mentioned in the DBConnection class.
Everytime I run the hadoop jar command I get error java.io.FileNotFoundException: db.properties (No such file or directory).
How can I resolve this?
Establishing DB connection in mapper:
Connection con = DBConnection.getConnection();
PreparedStatement pst = null;
String selectRows = "Select count(*) from sample";
Thanks

Try with this:
Prior Java 1.7:
InputStream input = YourClassName.class.getResourceAsStream("/db.properties");
try {
prop.load(input);
} catch (IOException ex) {
}finally{
input.close();
}
Java 1.7 and ahead:
try (InputStream input = YourClassName.class.getResourceAsStream("/db.properties")) {
prop.load(input);
} catch (IOException ex) {
}

Related

How to set class path and connect to DB2 using java

I don't know how to set the classpath in a java project.
This is the code that I run:
try { Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
} catch (ClassNotFoundException e) {
System.out.println("Please include Classpath Where your DB2 Driver is located");
e.printStackTrace();
return;
} System.out.println("DB2 driver is loaded successfully");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset=null;
boolean found=false;
try {
conn = DriverManager.getConnection("jdbc:db2:DB2PDBA","USERID","PASSWORD");
if (conn != null)
{
System.out.println("DB2 Database Connected");
}
else
{
System.out.println("Db2 connection Failed ");
}
and my Error is:
java.lang.ClassNotFoundException: com.ibm.db2.jdbc.app.DB2Driver
When we say put something within classpath, it means:
you should put the related jar files that your app needs to run
the entire application somewhere in your project.
Please follow the link below to create a DB2 application -> IBM Example about DB2
The only thing you need to do is to:
Add this into your project
change the credentials and db information
add the db2 jdbc jar file to your project

Java - JDBC MySQL connection issue

I am trying to establish a connection but for some reason I am having issues doing so.
I think it may be a syntax error - but I am not completely sure. I have commented out numerous syntax approaches I have tried. I used MySQL documentation for help, even when following their syntax i get problems.
When I run the code without the Connection the program jumps into the try. But as soon as I add the Connection code it jumps to the catch section of the code - therefore there must be an issue with the syntax.
Can anyone spot where I have gone wrong? Thank You in advance.
public void selectData()
{
try
{
Connection con = null;
//Accessing driver from the JAR file
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("com.mysql.jdbc");
//String a = "jdbc:mysql://localhost:3306/c3361434?profileSQL=true";
///String a = "jdbc:mysql://address=(protocol=tcp)(host=localhost)(port=3306)(user=root)(password=root)/c3361434";
//Connection con = DriverManager.getConnection("jdbc:mysql://address=(protocol=tcp)(host=localhost)(port=3306)(user=root)/c3361434");
//Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/c3361434", "root", "root");
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/c3361434?" + "user=root&password=root");
Output3.setText("Connection has been established");
/*
PreparedStatement statement = con.prepareStatement("SELECT * FROM RSA-data");
ResultSet result = statement.executeQuery();
while(result.next())
{
Output2.setText(result.getString(1));
Output3.setText("in the while loop");
}
*/
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Output4.setText("db fail");
}
}
try it:
Check if you mysql is running, in command line type:
mysqld --install
In java source:
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB?dontTrackOpenResources=true");
the command mysqld not reconized because you need to enter at path specific.
For example:
MS - DOS:
cd C:/MySQL/bin
C:/MySQL/bin>mysqld --install
Into folder bin there is the mysqld.exe
Do you got it ?

Class.forName(JDBC_DRIVER); Only working in 1 of my 2 programs when the code is the same. Why?

I have 1 basic program and 1 app, My basic program with this works ( DB_URL, USER, PASS, JDBC_DRIVER all correct and functional) and I am able to get information from my MySQL DB. The code consist of this:
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT distinct tags FROM items";
ResultSet rs = stmt.executeQuery(sql);
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
String itemRoles = rs.getString("tags");
//Add it to the ArrayList.
itemRolesList.add(itemRoles);
// Display values
System.out.print("TAGS: " + itemRoles + "\n");
}
// STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}// end finally try
}// end try
But when I try to apply this same code to my app (inside a Fragment in my OnCreateView()) I get this:
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
and it is at this line of code:
Class.forName(JDBC_DRIVER);
I added the "mysql-connector-java-5.1.23-bin.jar" and it is in my Reference Library in both my program and app. Does anyone know why inside my app it gives me this error?
Make sure that you have driver jar in your classpath.
Please verify that you have jar file inside lib folder is not the empty jar file.sometime during wrong confuguration it shows a empty jar file.
please go to project properties-->buildpath-->libraries
Check out the jar file you have added is not blank.

Getting Error database using Prperties file

I'm using properties file for Database,and here is mycode:
And I have set my database.prperties file in straight src folder.
here is my code(I'm applying this code in a jsp page):
Properties prop=new Properties();
InputStream inputStream=null;
try{
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
prop.load(inputStream);
}
finally{
if (inputStream != null) try { inputStream.close(); } catch (IOException ignore) {}
}
String driver=prop.getProperty("driver");
if (driver != null)
{
System.setProperty("driver", driver);
}
String url = prop.getProperty("url");
String username= prop.getProperty("username");
String password = prop.getProperty("password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,username,password); // Getting error at this line.
Statement stmt = con.createStatement();
String sql = "select * from info;";
ResultSet rs = stmt.executeQuery(sql);
System.out.println(sql);
Here is my properties file :
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/abc
username=crips
password=drift
But I'm getting this error java.sql.SQLException: Access denied for user 'root '#'localhost' (using password: YES) at line Connection con = DriverManager.getConnection(url,username,password);
Any inputs on this context will appreciated.
java.sql.SQLException: Access denied for user 'crips'#'localhost'
This means that the given user isn't been granted access to the database which you're attempting to connect. You'd need to issue the following SQL command with MySQL admin rights:
GRANT ALL ON abc.* TO 'crips'#'localhost' IDENTIFIED BY 'drift';
Note that the user name and password are case sensitive.
Also note that this has after all nothing to do with reading properties files. You'd have exactly the same problem when supplying username/password/database as hardcoded string variables.

How to read an sqlite database file in Java? (Package)

Okay i know i have to use the JDBC etc, but im not sure how to implement the jar into my code, i have a basic code to open the file etc, but how can i actually incorporate the sqlite jar alongside my java class file to be run anywhere?
So for example i have a folder with:
Test.class
new.db
sqlite.jar
In Test.class i have the basic connection and imports:
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:new.db");
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT empname FROM employee");
while (resultSet.next()) {
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("empname"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
So how can i have this simple little script portable?
Thanks
Add the sqlite.jar to you classpath:
java -cp .;./sqlite.jar Anima
This should work. If not - please show your error message.
Edit
tested and verified. Create/Have a folder with the following content:
./project
Anima.class
sqlite.jar
then cd to folder project and do a
java -cp .;./sqlite.jar Anima
It works as expected on my machine.

Categories

Resources