There are my codes.
MemberDAO.java
package sec05.ex01;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MemberDAO {
PreparedStatement pstmt;
Connection connection;
public List<MemberVO> listMembers() {
List<MemberVO> list = new ArrayList<>();
try {
connDB();
String query = "SELECT * FROM t_member";
System.out.println("preparedStatement: " + query);
pstmt = connection.prepareStatement(query);
ResultSet rs = pstmt.executeQuery(query);
while (rs.next()) {
String id = rs.getString("id");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String email = rs.getString("email");
Date joinDate = rs.getDate("joinDate");
MemberVO vo = new MemberVO();
vo.setId(id);
vo.setPwd(pwd);
vo.setName(name);
vo.setEmail(email);
vo.setJoinDate(joinDate);
list.add(vo);
}
rs.close();
pstmt.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
private void connDB() {
try {
String url = "jdbc:oracle:thin:#localhost:1521:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Loaded Oracle Driver");
connection = DriverManager.getConnection(url, "system", "oracle");
System.out.println("Connection created.");
System.out.println("PreparedStatement created.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
MemberServlet.java
package sec05.ex01;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;
#WebServlet("/member")
public class MemberServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
MemberDAO dao = new MemberDAO();
List<MemberVO> list = dao.listMembers();
out.print("<html><body>");
out.print("<table border=1><tr align='center' bgcolor='lightgreen'>");
out.print("<td>ID</td><td>PWD</td><td>NAME</td><td>EMAIL</td><td>DATE</td></tr>");
for (MemberVO memberVO : list) {
String id = memberVO.getId();
String pwd = memberVO.getPwd();
String name = memberVO.getName();
String email = memberVO.getEmail();
Date joinDate = memberVO.getJoinDate();
out.print("<tr><td" + id + "</td><td>" + pwd + "</td><td>" + name + "</td><td>" + email + "</td><td>" + joinDate + "</td></tr>");
}
out.print("</table></body></html>");
}
}
MemberVO.java
package sec05.ex01;
import java.sql.Date;
public class MemberVO {
private String id;
private String pwd;
private String name;
private String email;
private Date joinDate;
public MemberVO() {
System.out.println("MemberVO constructor called.");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
}
Error Message
But when I use console in IntelliJ, it works.
Database console in IntelliJ
Also, I added a jdbc library. (I'm using jdk17)
IntelliJ Library
I tried to change the library version to 8.
The ClassNotFoundException and NullPointerException occured.
Related
So i want to implement application which reads data from json format files. And I have created customer object for the data in json. And I want to send these object through kafka topic. So far i have successfully send String message to producer to consumer. But when i try to send object, in the consumer side, when I do .value().toString(). I got null value. The following is the code I have used:
This is producer:
public class MyProducer {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "xxxxxxxxx.KafkaJsonSerializer");
properties.put("acks", "1");
properties.put("retries", "2");
properties.put("batch.size", "16384");
properties.put("linger.ms", "1");
properties.put("buffer.memory", "33554432");
KafkaProducer<String, pharmacyData> kafkaProducer = new KafkaProducer<String, pharmacyData>(
properties);
String topic = "insights";
//try {
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(Paths.get("......./part.json"));
List<pharmacyData> pdata = new Gson().fromJson(reader, new TypeToken<List<pharmacyData>>() {}.getType());
//pdata.forEach(System.out::println);
reader.close();
//} catch (Exception e) {
//e.printStackTrace();
//}
for (pharmacyData data : pdata) {
kafkaProducer.send(new ProducerRecord<String, pharmacyData>(topic, data), new Callback() {
#Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
if (e == null) {
System.out.println(recordMetadata.partition() + "--" + recordMetadata.serializedValueSize());
} else {
e.printStackTrace();
}
}
});
}
kafkaProducer.close();
}
}
This is the customer object class:
public class pharmacyData {
private String load_date;
private String store_nbr;
private String state;
private String pmp_flag;
private String zero_flag;
private String submit_ts;
public pharmacyData(String load_date, String store_nbr, String state, String pmp_flag, String zero_flag, String submit_ts) {
this.load_date = load_date;
this.store_nbr = store_nbr;
this.state = state;
this.pmp_flag = pmp_flag;
this.zero_flag = zero_flag;
this.submit_ts = submit_ts;
}
public String getLoad_date() {
return load_date;
}
public void setLoad_date(String load_date) {
this.load_date = load_date;
}
public String getStore_nbr() {
return store_nbr;
}
public void setStore_nbr(String store_nbr) {
this.store_nbr = store_nbr;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPmp_flag() {
return pmp_flag;
}
public void setPmp_flag(String pmp_flag) {
this.pmp_flag = pmp_flag;
}
public String getZero_flag() {
return zero_flag;
}
public void setZero_flag(String zero_flag) {
this.zero_flag = zero_flag;
}
public String getSubmit_ts() {
return submit_ts;
}
public void setSubmit_ts(String submit_ts) {
this.submit_ts = submit_ts;
}
#Override
public String toString() {
return "pharmacyData{" +
"load_date='" + load_date + '\'' +
", store_nbr='" + store_nbr + '\'' +
", state='" + state + '\'' +
", pmp_flag='" + pmp_flag + '\'' +
", zero_flag='" + zero_flag + '\'' +
", submit_ts='" + submit_ts + '\'' +
'}';
}
}
this is the customer Serializer:
public class KafkaJsonSerializer implements Serializer {
private Logger logger = LogManager.getLogger(this.getClass());
#Override
public void configure(Map map, boolean b) {
}
#Override
public byte[] serialize(String s, Object o) {
byte[] retVal = null;
ObjectMapper objectMapper = new ObjectMapper();
try {
retVal = objectMapper.writeValueAsBytes(o);
} catch (Exception e) {
logger.error(e.getMessage());
}
return retVal;
}
#Override
public void close() {
}
}
This is the customer Deserializer:
public class KafkaJsonDeserializer implements Deserializer {
#Override
public void configure(Map map, boolean b) {
}
#Override
public Object deserialize(String s, byte[] bytes) {
ObjectMapper mapper = new ObjectMapper();
pharmacyData pdata = null;
try {
pdata = mapper.readValue(bytes, pharmacyData.class);
} catch (Exception e) {
e.printStackTrace();
}
return pdata;
}
#Override
public void close() {
}
}
This is consumer:
public class MyConsumer {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "kafka.kafka-cluster-shared.non-prod-5-az-scus.prod.us.walmart.net:9092");
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties.put("value.deserializer", "xxxxxxxx.KafkaJsonDeserializer");
properties.put("group.id", "consumer-group-1");
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "1000");
properties.put("auto.offset.reset", "earliest");
properties.put("session.timeout.ms", "30000");
KafkaConsumer<String, pharmacyData> consumer = new KafkaConsumer<>(properties);
String topic = "insights";
consumer.subscribe(Collections.singletonList(topic));
while (true) {
ConsumerRecords<String, pharmacyData> consumerRecords = consumer.poll(100);
for (ConsumerRecord<String, pharmacyData> consumerRecord : consumerRecords) {
System.out.println(consumerRecord.key() + "--" + consumerRecord.toString());
//System.out.println(consumerRecord.offset() + "--" + consumerRecord.partition());
}
}
}
}
Can someone please help me with the issues? Thank you very much!
Problem solved:
The solution of this issue just add a default constructor as below:
public pharmacyData() {
}
See this page for more details.
DBHELPER..........}}}}}}}}}
package com.example.userlap.model2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
public static final String Database="Model.db";
public DBHelper(Context context) {
super(context, Database, null, 1);
//SQLiteDatabase db=this.getWritableDatabase() ;
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ UserProfile.Users.TableName+" ("+ UserProfile.Users.Col_1+" Integer primary key AUTOINCREMENT,"+ UserProfile.Users.Col_2 +" TEXT,"+ UserProfile.Users.Col_3 +" TEXT,"+ UserProfile.Users.Col_4 +" TEXT,"+ UserProfile.Users.Col_5 +" TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if exists "+ UserProfile.Users.TableName);
onCreate(db);
}
public boolean addInfo(String uname,String DOb,String password,String Gender){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(UserProfile.Users.Col_2,uname);
cv.put(UserProfile.Users.Col_3,DOb);
cv.put(UserProfile.Users.Col_4,password);
cv.put(UserProfile.Users.Col_5,Gender);
long ans=db.insert(UserProfile.Users.TableName,null,cv);
if(ans==-1){
return false;
}
else
return true;
}
public boolean updateInfor(int id,String uname,String DOb,String password,String Gender){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(UserProfile.Users.Col_2,uname);
cv.put(UserProfile.Users.Col_3,DOb);
cv.put(UserProfile.Users.Col_4,password);
cv.put(UserProfile.Users.Col_5,Gender);
long ans=db.update(UserProfile.Users.TableName,cv,"UserInfo="+id,null);
if(ans==-1){
return false;
}
else
return true;
}
public List readAllInfor(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
public List readAllInfor(int id){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName+" where _ID="+id,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
public Integer deleteInfo(int id){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(UserProfile.Users.TableName,"_ID=?"+id,null);
}
}
USERPROFILE-----------------------------------------------------
package com.example.userlap.model2;
import android.provider.BaseColumns;
public class UserProfile {
private UserProfile() {
}
public final static class Users implements BaseColumns{
public static final String Col_1="_ID";
public static final String Col_2="userName";
public static final String Col_3="dateOfBirth";
public static final String Col_4="Password";
public static final String Col_5="Gender";
public static final String TableName="UserInfo";
}
}
HOME---------------------------------------------------
package com.example.userlap.model2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class Home extends AppCompatActivity {
DBHelper mydb;
Button register,login;
EditText id,pw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mydb=new DBHelper(this);
register=(Button)findViewById(R.id.button2);
login=(Button)findViewById(R.id.button) ;
id=(EditText)findViewById(R.id.editText);
pw=(EditText)findViewById(R.id.editText2);
login();
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i1=new Intent(Home.this,ProfileManagement.class);
startActivity(i1);
}
});
}
public void login(){
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<ui> list=(ArrayList<ui>)mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list.isEmpty()){
for(ui u:list){
if(u.getID()==Integer.parseInt(id.getText().toString()) && u.getPasssword().equals(pw.getText().toString())){
Intent i1=new Intent(Home.this,EditProfile.class);
startActivity(i1);
}
}
}
}
});
}
}
ProfileManagement----------------------------
package com.example.userlap.model2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class ProfileManagement extends AppCompatActivity {
Button edit,register;
EditText uname,pass,dob;
DBHelper mydb;
RadioButton type;
RadioGroup gender;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_management);
mydb=new DBHelper(this);
edit=(Button)findViewById(R.id.button6);
register=(Button)findViewById(R.id.button7);
uname=(EditText)findViewById(R.id.editText6);
pass=(EditText)findViewById(R.id.editText7);
dob=(EditText)findViewById(R.id.editText8);
gender=(RadioGroup)findViewById(R.id.radioGroup2);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i2=new Intent(ProfileManagement.this,EditProfile.class);
startActivity(i2);
}
});
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean res= mydb.addInfo(uname.getText().toString(),dob.getText().toString(),pass.getText().toString(),gtype(gender));
if(res=true){
Toast.makeText(ProfileManagement.this,"Successful",Toast.LENGTH_LONG).show();
}
else
Toast.makeText(ProfileManagement.this,"NOT ",Toast.LENGTH_LONG).show();
}
});
}
public String gtype(View v){
int res=gender.getCheckedRadioButtonId();
type=(RadioButton)findViewById(res);
return type.getText().toString();
}
}
EditProfile-----------------------------------------------------------
package com.example.userlap.model2;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
public class EditProfile extends AppCompatActivity {
Button search,edit,delete;
DBHelper mydb;
RadioButton type,male,female;
RadioGroup gender;
EditText uname,pass,dob,id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydb=new DBHelper(this);
//ArrayList<ui> list11=new ArrayList<>();
setContentView(R.layout.activity_edit_profile);
search=(Button)findViewById(R.id.button3);
edit=(Button)findViewById(R.id.button4);
delete=(Button)findViewById(R.id.button5);
uname=(EditText)findViewById(R.id.editText3);
pass=(EditText)findViewById(R.id.editText5);
dob=(EditText)findViewById(R.id.editText4);
gender=(RadioGroup)findViewById(R.id.radioGroup);
id=(EditText)findViewById(R.id.editText9);
male=(RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);
}
}
}
});
}
public String gtype(View v){
int res=gender.getCheckedRadioButtonId();
type=(RadioButton)findViewById(res);
return type.getText().toString();
}
}
ui-----------------------------------------------------------------------------------
package com.example.userlap.model2;
public class ui {
private String usename;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
private int ID;
private String Passsword;
private String DOB;
private String Gender;
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPasssword() {
return Passsword;
}
public void setPasssword(String passsword) {
Passsword = passsword;
}
public String getDOB() {
return DOB;
}
public void setDOB(String DOB) {
this.DOB = DOB;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
}
selecting a single record
public List readAllInfor(int id){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName+" where _ID="+id,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
-- Calling it
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor(Integer.parseInt(id.getText().toString()));
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);
Selecting all records
public List readAllInfor(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cus=db.rawQuery("Select * from "+ UserProfile.Users.TableName,null);
ArrayList<ui> list=new ArrayList<>();
if(cus.getCount()>0){
while (cus.moveToNext()){
ui ui1=new ui();
ui1.setID(cus.getInt(0));
ui1.setUsename(cus.getString(1));
ui1.setDOB(cus.getString(2));
ui1.setPasssword(cus.getString(3));
ui1.setGender(cus.getString(4));
list.add(ui1);
}
}
return list;
}
--calling it
ArrayList<ui> list11= (ArrayList<ui>) mydb.readAllInfor();
if(!list11.isEmpty()){
for(ui i:list11){
uname.setText(i.getUsename());
pass.setText(i.getPasssword());
dob.setText(i.getDOB());
if(i.getGender().equals("Male")){
male.setChecked(true);
}
else
female.setChecked(true);
-- USING CURSORS --
Selecting a single Record
public MovieDetailsVO getMovie(int movie_id) {
MovieDetailsVO movieDetails = new MovieDetailsVO();
SQLiteDatabase db = this.getReadableDatabase();
//specify the columns to be fetched
String[] columns = {KEY_MOVIE_ID, KEY_MOVIE_NAME, KEY_GENRE, KEY_YEAR, KEY_RATING};
//Select condition
String selection = KEY_MOVIE_ID + " = ?";
//Arguments for selection
String[] selectionArgs = {String.valueOf(movie_id)};
Cursor cursor = db.query(TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
movieDetails.setMovieId(cursor.getInt(0));
movieDetails.setMovieName(cursor.getString(1));
movieDetails.setGenre(cursor.getString(2));
movieDetails.setYear(cursor.getInt(3));
movieDetails.setRating(cursor.getDouble(4));
}
db.close();
return movieDetails;
}
Retrieving all Records
public List getAllMovies() {
List movieDetailsList = new ArrayList();
String selectQuery = "SELECT * FROM " + TABLE_NAME
+ " ORDER BY " + KEY_MOVIE_ID + " DESC";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
//if TABLE has rows
if (cursor.moveToFirst()) {
//Loop through the table rows
do {
MovieDetailsVO movieDetails = new MovieDetailsVO();
movieDetails.setMovieId(cursor.getInt(0));
movieDetails.setMovieName(cursor.getString(1));
movieDetails.setGenre(cursor.getString(2));
movieDetails.setYear(cursor.getInt(3));
movieDetails.setRating(cursor.getDouble(4));
//Add movie details to list
movieDetailsList.add(movieDetails);
} while (cursor.moveToNext());
}
db.close();
return movieDetailsList;
}
I would like to ask for a solution to my problem:
HTTP Status 500 - Internal Server Error
type Exception report
message Internal Server Error
description The server encountered an internal error that prevented it from
fulfilling this request.
exception
java.awt.HeadlessException
note The full stack traces of the exception and its root causes are available
in the GlassFish Server Open Source Edition 4.1.1 logs.
GlassFish Server Open Source Edition 4.1.1
There are a few tables in the database, but when I need to get records from the table "Student", it keeps showing this error.
domain file :
package domain;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {
private String studID;
private String studName;
private int studIC;
private String gender;
private String address;
private int contactNo;
public Student(){}
public Student(String studID){
this.studID = studID;
}
public Student(String studID, String studName, int studIC, String gender, String address, int contactNo){
this.studID = studID;
this.studName = studName;
this.studIC = studIC;
this.gender = gender;
this.address = address;
this.contactNo = contactNo;
}
public String getStudID(){
return studID;
}
public void setStudID(String studID){
this.studID = studID;
}
public String getStudName(){
return studName;
}
public void setStudName(String studName){
this.studName = studName;
}
public int getStudIC(){
return studIC;
}
public void setStudIC(int studIC){
this.studIC = studIC;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
public int getContactNo(){
return contactNo;
}
public void setContactNo(int contactNo){
this.contactNo = contactNo;
}
}
da file :
package da;
import domain.Student;
import java.sql.*;
import javax.swing.*;
public class StudentDA {
private String host = "jdbc:derby://localhost:1527/hostelDB";
private String user = "nbuser";
private String password = "nbuser";
private String tableName = "Student";
private Connection conn;
private PreparedStatement stmt;
public StudentDA(){
createConnection();
}
public void addRecord(Student student) {
String insertStr = "INSERT INTO " + tableName + " VALUES(?, ?, ?, ?, ?, ?)";
try {
stmt = conn.prepareStatement(insertStr);
stmt.setString(1, student.getStudID());
stmt.setString(2, student.getStudName());
stmt.setInt(3, student.getStudIC());
stmt.setString(4, student.getGender());
stmt.setString(5, student.getAddress());
stmt.setInt(6, student.getContactNo());
stmt.executeUpdate();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
public Student getRecord(String studID) {
String queryStr = "SELECT * FROM " + tableName + " WHERE StudentID = ?";
Student student = null;
try {
stmt = conn.prepareStatement(queryStr);
stmt.setString(1, studID);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
student = new Student(studID, rs.getString("StudentName"), rs.getInt("StudentIC"), rs.getString("Gender"), rs.getString("Address"), rs.getInt("ContactNo"));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
return student;
}
private void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn = DriverManager.getConnection(host, user, password);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
private void shutDown() {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
}
}
servlet :
String studID = request.getParameter("studID");
Student stud = studDA.getRecord(studID);
out.println(stud);
I had a problem with Pusher with Service application on Android.
When I using pusher on Application or Activity then It's working.
But I move it to Service and register with application in manifress like:
<service android:name=".services.PusherServices"/>
It's not working.
When i startService Pusher connected success with authorization, but It's not bind events when I call method for register Channel.
PusherServices.java
package vn.hemlock.winkle.pancake.services;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import com.pusher.client.Pusher;
import com.pusher.client.PusherOptions;
import com.pusher.client.channel.PrivateChannel;
import com.pusher.client.channel.PrivateChannelEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;
import com.pusher.client.util.HttpAuthorizer;
import net.windjs.android.utils.Encrypt;
import net.windjs.android.utils.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import vn.hemlock.winkle.pancake.abstracts.ChatEvents;
import vn.hemlock.winkle.pancake.contracts.ServicesURI;
import vn.hemlock.winkle.pancake.ui.Conversation;
import vn.hemlock.winkle.pancake.ui.Message;
import vn.hemlock.winkle.pancake.ui.Page;
/**
* Created by me866chuan on 5/4/15.
*/
public class PusherServices extends Service {
private String LOG_TAG = "Pusher Services";
private boolean pusherOn = false;
public final String PUSHER_KEY = "...";
private String userToken = "";
private String userID = "";
private final IBinder mBinder = new LocalBinder();
public boolean isPusherOn() {
return pusherOn;
}
public void setPusherOn(boolean pusherOn) {
this.pusherOn = pusherOn;
}
public String getUserToken() {
return userToken;
}
public void setUserToken(String userToken) {
this.userToken = userToken;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
#Override
public IBinder onBind(Intent intent) {
setUserID(intent.getStringExtra("userId"));
setUserToken(intent.getStringExtra("userToken"));
startPusher();
startService(intent);
return mBinder;
}
#Override
public boolean onUnbind(Intent intent) {
Log.e(LOG_TAG, "OFF");
stopService(intent);
return true;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
pusher.disconnect();
}
private Pusher pusher;
public void startPusher() {
HashMap<String, String> hash = new HashMap<>();
Log.e(LOG_TAG, "Token: "+getUserToken());
hash.put("authorization", getUserToken());
HttpAuthorizer authorizer = new HttpAuthorizer(ServicesURI.PUSHER_AUTHORIZER);
authorizer.setHeaders(hash);
PusherOptions options = new PusherOptions().setAuthorizer(authorizer);
pusher = new Pusher(PUSHER_KEY, options);
pusher.connect(new ConnectionEventListener() {
#Override
public void onConnectionStateChange(ConnectionStateChange change) {
Log.e(LOG_TAG, "State changed to " + change.getCurrentState() +
" from " + change.getPreviousState());
if(change.getCurrentState().toString().toUpperCase().equals("CONNECTED") || change.getCurrentState().toString().toUpperCase().equals("CONNECTING")){
setPusherOn(true);
}
else setPusherOn(false);
}
#Override
public void onError(String message, String code, Exception e) {
Log.e(LOG_TAG, "There was a problem connecting: " + message);
}
}, ConnectionState.ALL);
}
PrivateChannel userChannel;
public void userListenChanel(final Activity activity) {
if (pusher == null) return;
userChannel = pusher.subscribePrivate("private-" + Encrypt.stringMD5(getUserID()));
userChannel.bind("fetch_accounts", new PrivateChannelEventListener() {
#Override
public void onEvent(String channel, String event, final String data) {
Log.e(LOG_TAG, "Received event with data: " + data);
if (activity != null) activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
ChatEvents.getInstance().onNewPage(new Page((new JSONObject(data)).getJSONObject("page")));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onAuthenticationFailure(String s, Exception e) {
Log.e(LOG_TAG, "USER FETCH FAIL: " + s);
}
#Override
public void onSubscriptionSucceeded(String s) {
Log.e(LOG_TAG, "USER FETCH SUCCESS: " + s);
}
});
}
PrivateChannel pageChannel;
private String idPageChannelListen;
public void pageListenChanel(final Activity activity, String pageId) {
if (pusher == null) return;
idPageChannelListen = pageId;
Log.e(LOG_TAG, "Channel page: "+"private-" + Encrypt.stringMD5(pageId));
pageChannel = pusher.subscribePrivate("private-" + Encrypt.stringMD5(pageId));
pageChannel.bind("realtime_updates", new PrivateChannelEventListener() {
#Override
public void onEvent(String channel, String event, final String data) {
Log.e(LOG_TAG, "Real time update with data: " + data);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
final JSONObject finalJsonObject = jsonObject;
if(activity != null) activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (finalJsonObject == null) return;
try {
if (finalJsonObject.has("message")) ChatEvents.getInstance().onNewMessage(new Message(finalJsonObject.getJSONObject("message")));
else if (finalJsonObject.has("conversation")) ChatEvents.getInstance().onNewConversation(new Conversation(finalJsonObject.getJSONObject("conversation")));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onAuthenticationFailure(String s, Exception e) {
Log.e(LOG_TAG, "Real time update failed: " + s);
}
#Override
public void onSubscriptionSucceeded(String s) {
Log.e(LOG_TAG, "Real time update successed: " + s);
}
});
pageChannel.bind("fetch_conversations", new PrivateChannelEventListener() {
#Override
public void onEvent(String channel, String event, final String data) {
Log.e(LOG_TAG, "Received event with data: " + data);
if(activity != null) activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
ChatEvents.getInstance().onNewConversation(new Conversation((new JSONObject(data)).getJSONObject("conversation")));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public void onAuthenticationFailure(String s, Exception e) {
}
#Override
public void onSubscriptionSucceeded(String s) {
}
});
pageChannel.bind("fetch_comments", new PrivateChannelEventListener() {
#Override
public void onEvent(String channel, String event, String data) {
Log.e(LOG_TAG, "Received event with data: " + data);
}
#Override
public void onAuthenticationFailure(String s, Exception e) {
}
#Override
public void onSubscriptionSucceeded(String s) {
}
});
}
public void removeLisnterPage(String pageId) {
if (pusher != null) pusher.unsubscribe("private-" + Encrypt.stringMD5(pageId));
idPageChannelListen = "";
}
public void reconnectPusher() {
if (pusher != null) {
pusher.disconnect();
pusher.connect();
}
}
public void disconnectPusher() {
if (pusher != null) pusher.disconnect();
}
public String getIdPageChannelListen() {
return idPageChannelListen;
}
public class LocalBinder extends Binder {
public PusherServices getService() {
// Return this instance of LocalService so clients can call public methods
return PusherServices.this;
}
}
}
I'm trying to implement a hadoop job, that counts how often a object (Click) appears in a dataset.
Therefore i wrote a custom file input format. The record reader seems to read only the first line of the given file and the close the input stream.
Here is the code:
The Pojo class:
package model;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
public class Click implements WritableComparable<Click> {
private String user;
private String clickStart;
private String date;
private String clickTarget;
#Override
public void write(DataOutput out) throws IOException {
out.writeUTF(user);
out.writeUTF(clickStart);
out.writeUTF(date);
out.writeUTF(clickTarget);
}
#Override
public void readFields(DataInput in) throws IOException {
user = in.readUTF();
clickStart = in.readUTF();
date = in.readUTF();
clickTarget = in.readUTF();
}
public int compareTo(Click arg0) {
int response = clickTarget.compareTo(arg0.clickTarget);
if (response == 0) {
response = date.compareTo(arg0.date);
}
return response;
}
public String getUser(String user) {
return this.user;
}
public void setUser(String user) {
this.user = user;
}
public String getClickStart() {
return clickStart;
}
public void setClickStart(String clickStart) {
this.clickStart = clickStart;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getClickTarget() {
return clickTarget;
}
public void setClickTarget(String clickTarget) {
this.clickTarget = clickTarget;
}
public String toString() {
return clickStart + "\t" + date;
}
}
Here is the FileInputFormat class:
package ClickAnalysis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import model.Click;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.util.StringUtils;
import org.apache.tools.ant.types.CommandlineJava.SysProperties;
public class ClickAnalysisInputFormat extends FileInputFormat<Click, IntWritable>{
#Override
public RecordReader<Click, IntWritable> createRecordReader(
InputSplit split, TaskAttemptContext context) throws IOException,
InterruptedException {
System.out.println("Creating Record Reader");
return new ClickReader();
}
public static class ClickReader extends RecordReader<Click, IntWritable> {
private BufferedReader in;
private Click key;
private IntWritable value;
#Override
public void initialize(InputSplit inputSplit, TaskAttemptContext context) throws IOException, InterruptedException {
key = new Click();
value = new IntWritable(1);
System.out.println("Starting to read ...");
FileSplit split = (FileSplit) inputSplit;
Configuration conf = context.getConfiguration();
Path path = split.getPath();
InputStream is = path.getFileSystem(conf).open(path);
in = new BufferedReader(new InputStreamReader(is));
}
#Override
public boolean nextKeyValue() throws IOException, InterruptedException {
String line = in.readLine();
System.out.println("line: " + line);
boolean hasNextKeyValue;
if (line == null) {
System.out.println("line is null");
hasNextKeyValue = false;
} else {
String[] click = StringUtils.split(line, '\\', ';');
System.out.println(click[0].toString());
System.out.println(click[1].toString());
System.out.println(click[2].toString());
key.setClickStart(click[0].toString());
key.setDate(click[1].toString());
key.setClickTarget(click[2].toString());
value.set(1);
System.out.println("done with first line");
hasNextKeyValue = true;
}
System.out.println(hasNextKeyValue);
return hasNextKeyValue;
}
#Override
public Click getCurrentKey() throws IOException, InterruptedException {
return this.key;
}
#Override
public IntWritable getCurrentValue() throws IOException, InterruptedException {
return this.value;
}
#Override
public float getProgress() throws IOException, InterruptedException {
return 0;
}
public void close() throws IOException {
in.close();
System.out.println("in closed");
}
}
}
The Mapper class:
package ClickAnalysis;
import java.io.IOException;
import model.Click;
import model.ClickStartTarget;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;
import org.jruby.RubyProcess.Sys;
public class ClickAnalysisMapper extends Mapper<Click, IntWritable, Click, IntWritable> {
private static final IntWritable outputValue = new IntWritable();
#Override
protected void map(Click key, IntWritable value, Context context) throws IOException, InterruptedException {
System.out.println("Key: " + key.getClickStart() + " " + key.getDate() + " " + key.getClickTarget() + " Value: " + value);
outputValue.set(value.get());
System.out.println(outputValue.get());
context.write(key, outputValue);
System.out.println("nach context");
}
}
Partitioner class:
package ClickAnalysis;
import model.Click;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner;
public class ClickAnalysisPartitioner extends Partitioner<Click, IntWritable> {
#Override
public int getPartition(Click key, IntWritable value, int numPartitions) {
System.out.println("in Partitioner drinnen");
int partition = numPartitions;
return partition;
}
}
Hadoop Job, which is triggered via an Restful web service call in a servlet container, but this shouldn't be the problem:
package ClickAnalysis;
import model.Click;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
public class ClickAnalysisJob {
public int run() throws Exception {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "ClickAnalysisJob");
job.setJarByClass(ClickAnalysisJob.class);
// Job Input path
FileInputFormat.setInputPaths(job, "hdfs://localhost:9000/user/hadoop/testdata1.csv");
// Job Output path
Path out = new Path("hdfs://localhost:9000/user/hadoop/clickdataAnalysis_out");
FileOutputFormat.setOutputPath(job, out);
out.getFileSystem(conf).delete(out,true);
job.setMapperClass(ClickAnalysisMapper.class);
job.setReducerClass(Reducer.class);
job.setPartitionerClass(ClickAnalysisPartitioner.class);
//job.setReducerClass(ClickAnalysisReducer.class);
job.setInputFormatClass(ClickAnalysisInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
job.setOutputKeyClass(Click.class);
job.setOutputValueClass(IntWritable.class);
job.setMapOutputKeyClass(Click.class);
job.setMapOutputValueClass(IntWritable.class);
System.out.println("in run drinnen");
//job.setGroupingComparatorClass(ClickTargetAnalysisComparator.class);
job.setNumReduceTasks(1);
int result = job.waitForCompletion(true)? 0:1;
return result;
}
}
Next the dataset (example):
/web/big-data-test-site/test-seite-1;2014-07-08;ein ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein anderes ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein anderes ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein drittes ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein viertes ziel
/web/big-data-test-site/test-seite-1;2014-07-08;ein ziel
When I run the program the syso's are showing following:
in run drinnen
Creating Record Reader
Starting to read ...
line: /web/big-data-test-site/test-seite-1;2014-07-08;ein ziel
/web/big-data-test-site/test-seite-1
2014-07-08
ein ziel
done with first line
true
Key: /web/big-data-test-site/test-seite-1 2014-07-08 ein ziel Value: 1
1
in closed
analyze Method: 1
From that i conclude that the record reader only reads the first line.
Why is this happening and how is it fixed?