Sunday, October 31, 2021

Design Is Not My Thing

I learned long ago that design wasn't my thing. I'm not good at design. I'm actually terrible at it.

One would think I'd learn something over the years but that's just not possible. Well I shouldn't say it isn't possible, it just isn't plausible. Design just isn't my thing. I don't do design. I don't make things look pretty, I make things work. Someone else can make it look pretty.

JList Populated From String Array


import javax.swing.JFrame;
import javax.swing.JList;

public class ListTest {

public ListTest() {
JFrame frame = new JFrame();
frame.setTitle("JList Test");
frame.setSize(640,480);
frame.setLocationRelativeTo(null);

String[] items = {"One", "Two", "Three"};
JList list = new JList(items);

frame.add(list);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {
new ListTest();
}
}

Saturday, October 30, 2021

Programming

Programming has a lot of structure to it. There are somedays that are good and somedays that are bad. Either way you program something to better the lives of others. Well you hope you better the lives of others. If you don't, what's the use of programming?

Friday, October 29, 2021

Multiplication Table in Java

Nested for loops.


for (int i = 1; i <=12; i++) {
for (int j = 1; j <= 12; j++) {
System.out.printf("%d * %d = %d\n", i, j, i*j);
}
}

Thursday, October 28, 2021

Hello World Part 2

Python takes a much easier approach to printing Hello World!

print("Hello World!")

It doesn't have a lot of the other code Java had.

Wednesday, October 27, 2021

Life Isn&#39;t Easy

Life isn't the easiest thing in the world. Somedays are downright difficult while other days are simply unbearable. How you respond to those difficult trying days is what makes you who you are. There's no other way around it.

Sometimes you might catch the ball, othertimes you might fumble. But how you take your own life afterwards is what causes you to either win or lose the game.

Netbeans

Been playing around with Netbeans lately. It's an okay tool. I enjoy the form creator the most in it. It's easy to create a form and then display whatever it is you wanted to create. I made a handy dandy food calorie tracker using this method.

All in all it's not bad. The code it creates is ugly as hell, but that's okay for small simple projects that I don't want to think about creating a form for manually.

Tuesday, October 26, 2021

A Question About Churches Part 2

If all churches have some truth in them as the LDS Church teaches. Isn't that enough? I mean isn't that belief system enough for people to go to a church and simply believe and worship as they so desire? I don't understand why one church has to be the true and living church.

This goes back to the thought that the LDS Church hasn't been fully restored and is continuing to be restored. When Joseph Smith said that he had restored the Gospel, I guess he wasn't done? Left that bit to other prophets down the line?

It doesn't quite make sense to me.

Monday, October 25, 2021

Source Code for Simple Database Program

This database program is simple, it uses the sqlite jar for connecting to the SQLite database.

It can store a file's text in a database, update that text, and extract that text back out to it's original file name.

It's pretty quick and dirty. There might be better ways to handle some of it, but I just wanted to make sure it would do what I wanted it to do.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Sample {

private Connection conn = null;

public Sample() {

}

public void init() {

try {
String url = "jdbc:sqlite:sample.db";
conn = DriverManager.getConnection(url);

} catch (SQLException e) {
e.printStackTrace();
}
}

public void saveData(String filename) {
try {
String fileContent = new String(Files.readAllBytes(Paths.get(filename)));
String sql = "insert into files(name, content) values(?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, filename);
pstmt.setString(2, fileContent);
pstmt.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void updateData(String filename) {
try {
String fileContent = new String(Files.readAllBytes(Paths.get(filename)));
String sql = "update files set content = ? where name = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fileContent);
pstmt.setString(2, filename);
pstmt.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void exportData(String id) {
StringBuilder sb = new StringBuilder();
String sql = "select * from files where id = ?";
ResultSet rs = null;
try {
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, id);
rs = pst.executeQuery();

while (rs.next()) {
Path path = Paths.get(rs.getString("name"));
Files.writeString(path, rs.getString("content"), StandardCharsets.UTF_8);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
if (args.length != 2) {
System.err.println("ERROR: You must provide two arguments.");
System.err.println("insert or update");
System.err.println("filename to insert or update");
return;
}
Sample sample = new Sample();
sample.init();
if (args[0].equals("insert")) {
sample.saveData(args[1]);
} else if (args[0].equals("update")) {
sample.updateData(args[1]);
} else if (args[0].equals("export")) {
sample.exportData(args[1]);
}

}
}

Sunday, October 24, 2021

Needed

Ever feel like you aren't needed?

It's quite a bad feeling to have.

A Question About Churches

I was asked by my mother if I felt any other church out there would be good for me. That was the bascics of the question. I don't recall the question exactly. I said that I didn't know.

I have no desire to join any other church at the moment. Part of me is scared. Another part ofme still believes that it would be a slap to the face of God if I were to join another church for that would require baptism. Again we're struggling with the whole authority thing.

Another part of me wonders, "what if" it's all true and I'm screwing myself over.

So many thoughts and concerns are coming to mind. I simply do not know the answer to any of them.

Saturday, October 23, 2021

Grace

Grace is needed in this life and in the life to come. For Christ died for our sins, and rose the third day according to the scriptures. Without the gift that Christ brings to us, we cannot go home to that God who gave us life. Without Christ we would be forever damned to toil in hell. Without Christ we could not sing to His priases and become one with Him and His Father, our Father.

Some would have you believe that we can overcome the blocks of death all by ourselves through works. This is not so, for that would be denying Christ of what He did for us. That would be saying that we can do it all for ourselves and we wouldn't need a Savior. That is not the case.

The Bible clearly states that we are to be saved through grace not of works. (Ephesians 2:8-9)

Friday, October 22, 2021

Formatting a Date in Java

For some reason my installation of Java wouldn't let me use SimpleDateFormat to format my dates. So I had to google a different way to do this and found the following works as well. The Date class has a bunch of issues with it to begin with, so this is much cleaner and better as well.


LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.ENGLISH);
String formattedString = df.format(ldt);

Religious Beliefs

People are always asking what I believe in now. Am I still a Christian etc. Of course I'm still a Christian. I just don't believe in one religion anymore. I have my KJV Bible and I think that's enough for now. It's just me and the Bible at this point in time.

I think it's okay to believe in the Bible. It does have a lot of teachings in it that are lost on a lot of people. Naturally people only read and see what they want to from the book. Trying to understand every aspect of it might not always be possible. So here's what you do. You begin reading starting with John and then trhough all the letters. Tackle Revelations if you so desire, and then swing back around for the rest of the gospels.

This way of studying is going good so far.

Populate JTable from ResultSet

Every once in a while we need to populate data in a JTable. Here's a simple to use DefaultTableModel for populating a JTable from a ResultSet.


public DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {

ResultSetMetaData metaData = rs.getMetaData();

Vector columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}

Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}

DefaultTableModel dtm = new DefaultTableModel(data, columnNames) {

private static final long serialVersionUID = 1L;

@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};

return dtm;

}

Thursday, October 21, 2021

Connect to a SQLite Database in Java

Sometimes you need to connect to a database. Here is some code that will allow you to connect to an SQLite database using Java.


public static Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:sqlite:sample.db");
} catch (SQLException sqle) {
sqle.printStackTrace();
Logger.getLogger(SQLite.class.getName()).log(Level.SEVERE, null, sqle);
}
return conn;
}

Life

There once was a life
it seemed to sway
in the beauty therein
it was there to save the day

Some would beleive
it not being such amount
yet life without purpose
no it wouldn't count.

Authority To Baptize

Halfway through my mission I began to feel doubts. My first ever doubt was why did it have to take authority to baptize people. If a person's faith was strong, wouldn't that be enough for God to accept that person's baptism? I struggled with this thought for a while. I still struggle with this thought to this day. Why does it require authority from God to baptize someone?

Wednesday, October 20, 2021

Religion

There are times when a religion just doesn't work anymore for you. There are times when you have to move on from that religion or religious belief system. It can be heart breaking to many, even your parents and family. In fact I'm sure it will be heart breaking to many. But in the end you have to do what's right for you.

No one can tell you how to feel when it comes to religion and religious beliefs. I have come to know this after being out of religion for a good number of years. I'm sure this will come to no shock to anyone that I wasn't really ever that religious to begin with.

I figure I might as well chronolog my journey so I can best make sense of it myself.

Trying

If life has taught me anything it’s that I can’t expect tomorrow to be what I expect it to be. There are days where this is most relevant. I simply cannot think that tomorrow will be the same as today. It is quite a problem.

Somedays are better than others. Other days are worse than some.

I manage to get through it all somehow of course I don’t know how I do this considering that my life feels like a jumbled pulp of a mess at times. But I simply try to get by.

I think that’s the key is trying to get by. That’s all which can be expected of me.

So as long as I breathe I shall try.

Tuesday, October 19, 2021

Capture Screenshot in Java

Sometimes you just need to capture a screenshot in Java. Well look no further here's the code in question!


import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScreenShot {

public static void main(String[] args) throws IOException {
BufferedImage image;
try {
image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("screenshot.png"));
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
}

A Window Frame In Java

There are two kinds of programs in Java. One that runs on the commandline. The other runs as a GUI. Today we'll be focusing on GUIs.

In order to work with a GUI, first you need to create a window. We use a JFrame in order to do this.


import javax.swing.JFrame;

public class Window extends JFrame {

public Window() {
super("My Window");
setSize(640, 480);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
new Window();
}
}

Files Table in sql for SQLite3

Here's some code I'm working on for a sqlite3 database to store backup file content. Might add a BLOB type later for storing binary data. Not sure yet.


PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE files (id INTEGER PRIMARY KEY,
name TEXT,
content TEXT,
created_ts TEXT,
modified_ts TEXT);
CREATE TABLE files_log(id INTEGER PRIMARY KEY,
fileid TEXT,
name TEXT,
content TEXT,
created_ts TEXT,
modified_ts TEXT,
dt TEXT);
CREATE TRIGGER files_trig AFTER INSERT ON files
BEGIN
update files SET created_ts = datetime('now','localtime') WHERE id = NEW.id;
END;
CREATE TRIGGER files_update_trig AFTER UPDATE ON files
BEGIN
update files SET modified_ts = datetime('now','localtime') WHERE id = OLD.id;
insert into files_log(fileid, name, content, created_ts, modified_ts)
values(OLD.id, OLD.name, OLD.content, OLD.created_ts, OLD.modified_ts);
END;
CREATE TRIGGER files_log_update_trig AFTER INSERT ON files_log
BEGIN
update files_log SET dt = datetime('now', 'localtime') WHERE id = NEW.id;
END;
COMMIT;

Generating a Random Number in Java

Here's how we generate a random number in Java. It's quite simple. First we import java.util.Random. Then we call that class and grab the next int with an upper bound of 25. That is to say we pick a number from zero to twenty-five. It cannot go below zero it cannot go above twenty-five.


import java.util.Random;

public class RandomNumber {

public static void main(String[] args) {
Random rand = new Random();
int int_random = rand.nextInt(25);

System.out.println(int_random);
}

}

Monday, October 18, 2021

Sometimes You Just Have To Believe

There are days where you must beleive that something will work out. You don't have time to ask how it will work out, you just have to believe that it will.

No one asks how God created the Earth in six days. Just that He did it. We choose to believe this was the case.

So yes there are many things in life that make us wonder at times how something is accomplished. But for the most part we don't need to wonder such things and just believe that everything will go according to plan.

If Statements In Java

If statements are at the core of any Java program. They allow you to make decisions in your code.


public class Cake {

final static boolean I_HATE_CAKE = false;

public static void main(String[] args) {
// TODO Auto-generated method stub
if (I_HATE_CAKE) {
System.out.println("I really don't like cake. I hate it.");
} else {
System.out.println("I enjoy cake.");
}
}

}

Hello World!

No matter what programming language you begin with, you'll probably write a Hello World program in it. Here is one written in Java.


public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello World!");
}
}

Today is a day

 Today is definitely a day. I'm not sure there is anything good which can be said about it. Other than the fact that it's another week. Another week to feel stressed and other things. But well you know how this all works right? Some would have you believe that life comes and goes long into that great night never to be heard from again. But that's now how any of it works.

Ever feel like no one is listening?

 Ever have that feeling that no one is listening to you? Yeah, that feeling. It can be a strong feeling to have, a hurtful feeling also. The...