MySQL error store boolean value into tinyint - java

Class Code
this is some of code from Class Code that may help.
private ResultSet rs;
private Connection cn;
private Statement st;
public void insertData(String data)
{
try
{
st.executeUpdate(data);
{
JOptionPane.showMessageDialog(null, "Data berhasil Disimpan");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Gagal Insert Data");
}
}
InsertDaftar Class
public class InsertDaftar implements DaftarInterface {
public String nama;
public boolean kuasa;
Code cd = new Code();
public void setNama(String nama){
this.nama=nama;
}
public void setKuasa(Boolean kuasa){
this.kuasa=kuasa;
}
public void Akun(){
String data = "INSERT INTO akun (Nama,Kuasa)"+"values('"+this.nama+"','"+this.kuasa+"')";
cd.insertData(data);
}
I have created some code boolean for radio button.
boolean akun_kuasa;
if (admin.isSelected()){
akun_kuasa=true;
}
if (teller.isSelected()){
akun_kuasa=false;
}
//todo
InsertDaftar id = new InsertDaftar()
id.setNama(akun_nama.getText());
id.setKuasa(akun_kuasa);
there are warning message on
id.setKuasa(akun_kuasa);
Warning in Netbeans.
Initialize variable of akun_kuasa
I have tried to change type of "akun_kuasa" into int,
and changed akun_kuasa into 0 and 1, but there still error.
I have searched this problem. but there are to many about BOOLEAN or TinyInt.
NOTE: id is an object that have a method to store into database.

akun_kuasa can be undefined after the if's
Set it to false first? Only u would know
It is a good compiler warning to u
akun_kuasa is declared but potentially undefined
when u call the setter

Related

Do I have to create another RowMapper?

I'm developing this application to fetch data from a single table from an existing Oracle database.
Here we've got the entity:
public class OrdemDeServicoCount {
private Long ordensInternas;
private Long ordensAtrasadas;
// assume getters and setters
}
The mapper:
public class OrdemMapper implements RowMapper<OrdemDeServicoCount> {
#Override
public OrdemDeServicoCount mapRow(ResultSet rs, int rowNum) throws SQLException {
OrdemDeServicoCount ordens = new OrdemDeServicoCount();
ordens.setOrdensInternas(rs.getLong("ordensInternas"));
// ordens.setOrdensAtrasadas(rs.getLong("ordensAtrasadas"));
return ordens;
}
}
And finally, the DAO:
public class OrdemDAO {
private JdbcTemplate jdbcTemplate;
public OrdemDAO(JdbcTemplate jdbcTemplate) {
super();
this.jdbcTemplate = jdbcTemplate;
}
public List<OrdemDeServicoCount> countOrdensInternasSemEncerrar() {
String sql = "SELECT COUNT(a.nr_sequencia) AS ordensInternas FROM MAN_ORDEM_SERVICO a "
+ "WHERE a.IE_STATUS_ORDEM IN (1,2) AND a.NR_GRUPO_PLANEJ IN (21)";
List<OrdemDeServicoCount> ordens = jdbcTemplate.query(sql, new OrdemMapper());
return ordens;
}
By the way, you all must know that if I declare uncomment the line ordens.setOrdensInternas(rs.getLong("ordensInternas")); in the mapper, I would get an error, because in my DAO, I'm not using that field.
But what if I need to create another method that uses just the ordensInternas field? Then again, I'd get an error...
So, my doubt here is: if I need to use the ordensAtrasadas field from the entity, will I have to create another class just to implement another mapper? Or is there a way that I can do any conditional in my current OrdemMapper class?
Just put your assignments in individual try-catch statements.
public class OrdemMapper implements RowMapper<OrdemDeServicoCount> {
#Override
public OrdemDeServicoCount mapRow(ResultSet rs, int rowNum) throws SQLException {
OrdemDeServicoCount ordens = new OrdemDeServicoCount();
try {
ordens.setOrdensInternas(rs.getLong("ordensInternas"));
} catch (SQLException ex) {
// This will happen if the columnIndex is invalid among other things
}
try {
ordens.setOrdensAtrasadas(rs.getLong("ordensAtrasadas"));
} catch (SQLException ex) {
// This will happen if the columnIndex is invalid among other things
}
return ordens;
}
}

variable value in class is not getting loaded properly

Below is my code snippet,this piece of code will be called from a workitem with the needed input parameters.The problem which Iam facing is the value of ExcpStatus variable is not getting loaded properly,my workitems will fail sometime and the value of finalOpStatus will be fail making the value of ExcpStatus to YES.Now second time if i call this code (after ExcpStatus is changed to YES) the value of ExcpStatus should be reloaded as NO but it takes the previous changed value YES...Can someone help me in this....
public abstract class SampleWorkItemHandler implements WorkItemHandler {
public SampleWorkItemHandler()
{
this.sb_serviceTaskName="default";
}
public SampleWorkItemHandler(String taskName)
{
this.sb_serviceTaskName=taskName;
}
protected String ExcpStatus="NO";
protected String loggerCorrelationId = "";
private String startLoggerInsertStr="";
public void executeWorkItem(WorkItem workitem, WorkItemManager manager) {
try{
logger.info("the exception staus is::"+ExcpStatus);
logger.info("the loggerCorrelationId is::"+loggerCorrelationId);
String finalOpStatus=SyntBotsBPMOutputValidatorUtil.validateResponse(handlerResponseParams);
if(finalOpStatus.equalsIgnoreCase("Fail"))
{
ExcpStatus="YES";
}
}catch(Exception e){
System.out.println("Exception Stack Trace:::"+e.getMessage());
}
}

How do I call variables from a Java class in another Class

If I have a class with prepared statements, how do I call them when a user presses a button.
class updateeButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
name = displayName.getText();
price = bookPrice.getText();
Queries update = new Queries();???????????????????
}
}
Prepared Statement in different class
public int update(String price, String name) throws SQLException {
ps2.setString(1, name);
ps2.setString(2, price);
System.out.print("Update - ");
return ps2.executeUpdate();
}
Create an object and invoke the update method with parameters as shown below:
public void actionPerformed(ActionEvent e) {
name = displayName.getText();
price = bookPrice.getText();
Queries update = new Queries();
update.update(price, name);//invoke the method using the object
}

Error in retrieving data which contains null from Cassandra Database using Spark/Cassandra connector

I want to retrieve data from Cassandra using Cassandra/spark Connector in java. Some of values in table are null. I use
JavaRDD<String> rdd = javaFunctions(sc).cassandraTable("test", "Sportman", Sportman.class)
.map(new org.apache.spark.api.java.function.Function<Sportman, String>() {
#Override
public String call(Sportman person) throws Exception {
return person.toString();
}
});
I got this error when trying to run the program.
TypeConversionException: Failed to convert column accomplished of table test.Sportman to java.lang.Boolean: null
The I define Sportman class like this:
public static class Sportman implements Serializable{
private UUID rowid;
private Boolean accomplished;
public Sportman(){}
public UUID getRowid() {
return rowid;
}
public void setRowid(UUID rowid) {
this.rowid = rowid;
}
public Boolean getAccomplished() {
return accomplished;
}
public void setAccomplished(Boolean accomplished) {
this.accomplished = accomplished;
}
I don't know how to change get method to fix this error. any help would be appreciated.

Passing static variable value to another class

In my Java Application Im trying to create user groups and assign permission to users depending on the user group they belongs to. This is how I programmed. When User Login to the system, grab the user name and store in a static variable. When user opens any Form, get the user name from static variable and check its group and permissions. Depending on the permissions disable and enable some components of the form. Here Im having problem retrieving the value from static variable stgroupName. Method checkuserrights() at ItmMgt class not getting the current user's name. Can someone please assist me to understand whats the wrong here. I guess, there should be many better ways to do this. So any suggestions welcome.
public class Login extends javax.swing.JInternalFrame {
public static String USERNAME;
USERNAME = txtUserName.getText(); // get the user name to static variable
}
public class ItemMgt extends javax.swing.JInternalFrame {
public ItemMgt() {
initComponents();
checkuserrights();
genarateID();
}
private void checkuserrights() {
try {
Usergroup usergroup = UserGroupController.getUserRights(Login.USERNAME);// check the user's rights passing user name from static variable.
if (usergroup != null) {
btnDelete.setEnabled(usergroup.isDeleteItem());
btnAdd.setEnabled(usergroup.isAdditem());
btnUpdate.setEnabled(usergroup.isUpdateitem());
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
}
}
public class UserGroupController {
public static Usergroup getUserRights(String username) throws ClassNotFoundException, SQLException {
Connection conn = DBConnection.conn();
String sql = "select * from UserGroup where uGroupName = ?";
Object[] values = {username};
ResultSet res = DBHandller.getData(sql, conn, values);
while (res.next()) {
String grpName = res.getString("uGroupName");
boolean additem = res.getBoolean("addItem");
boolean delitem = res.getBoolean("delteItem");
boolean upitem = res.getBoolean("editItem");
Usergroup ugroup = new Usergroup(grpName, additem, delitem, upitem);
return ugroup;
}
return null;
}
}
A static variable is by definition a gloabl object. When you change the value in one instance, then the value of all other instances of that object will have the same value. That's the point of using a static.
It sounds to me that your object design is wrong and you should use an object having information about a user which would include the groupname. This object you can pass around in your code.

Categories

Resources