I am having to connect an access database to a netbeans project and create a program that allows a user to search for a football player's name, and have their results displayed on the screen. My problem is that when I do the setTexts at the end, the labels simply turn blank. I do not receive any error messages.
I don't know whether the problem lies in the linking to the database or in parsing the parameters, or somewhere else?
Connection conn = null;
PreparedStatement pst = null;
ResultSet rst = null;
String temp = new String();
String playerID = null;
String name = null;
String surname = null;
String shirtNo = null;
String height = null;
String prefFoot = null;
String nation = null;
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
conn = DriverManager.getConnection("jdbc:ucanaccess://Database4.accdb");
String sql = "SELECT * FROM Players WHERE Name = (?) and Surname = (?)";
pst = conn.prepareStatement(sql);
pst.setString(1, txtfieldFirst.getText());
pst.setString(2, txtfieldSecond.getText());
rst = pst.executeQuery();
if(rst.next())
{
int count = 0;
while(rst.next())
{
playerID = rst.getString("PlayerID");
name = rst.getString("Name");
surname = rst.getString("Surname");
shirtNo = rst.getString("ShirtNo");
height = rst.getString("Height (Metres)");
prefFoot = rst.getString ("PrefFoot");
nation = rst.getString ("Nation");
}
}
conn.close();
}
catch (Exception e)
{
System.out.println(e);
}
Connection con = null;
PreparedStatement pstt = null;
ResultSet rs = null;
String temp2 = new String();
String league = null;
String DOB = null;
String club = null;
boolean tec = false;
String goals15 = null;
String goals16 = null;
String goals17 = null;
String assists15 = null;
String assists16 = null;
String assists17 = null;
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection("jdbc:ucanaccess://Database4.accdb");
String sqll = "SELECT * FROM Stats WHERE PlayerID = (?)";
pstt = con.prepareStatement(sqll);
pstt.setString(1, playerID);
rs = pstt.executeQuery();
if(rs.next())
{
int count = 0;
while(rs.next())
{
league = rs.getString("League");
DOB = rs.getString("DOB");
club = rs.getString("Club");
goals15 = rs.getString("Goals15/16");
goals16 = rs.getString("Goals16/17");
goals17 = rs.getString("Goals17/18");
assists15 = rs.getString("Assists15/16");
assists16 = rs.getString("Assists16/17");
assists17 = rs.getString("Assists17/18");
}
}
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
babyStats pps = new babyStats(league, DOB, club, goals15, goals16, goals17, assists15, assists16, assists17);
babyPlayers ps = new babyPlayers(name, surname, shirtNo, height, prefFoot, nation);
PlayerScreen p = new PlayerScreen(ps, pps); //connection to other screen
p.setVisible(true);
this.setVisible(false);
public PlayerScreen(babyPlayers obj, babyStats objj) //parsed as paramters
{
initComponents();
lblName.setText(obj.getName());
lblSurname.setText(obj.getName());
lblShirtNo.setText(obj.getShirtNo());
lblDOB.setText(objj.getDOB());
lblHeight.setText(obj.getHeight());
lblPFoot.setText(obj.getPreFoot());
lblClub.setText(objj.getClub());
lblNation.setText(obj.getNation()); //these setTexts just make the labels blank
lblGoals16.setText(objj.getGoals1516());
lblGoals17.setText(objj.getGoals1617());
lblGoals18.setText(objj.getGoals1718());
lblAssists16.setText(objj.getAssists1516());
lblAssists17.setText (objj.getAssists1617());
lblAssists18.setText (objj.getAssists1718());
}
I expect the labels to be set with the details coming from the database, but they turn blank instead. I would really appreciate any help. *Update, while debugging, I used system.out.println to print one of the names, and the result came out as null. *Update, I fixed it, the while loops were not meant to be there.
Related
I have a table called "snomed_conceptdata" from which iam trying to retrieve a column called "id" which has around 454772 rows present in it.And iam using this 'id' to get only certain rows from another table called snomed_descriptiondata where the conceptid column value equals this "id" and then inserting those rows to a another table called snomedinfo_data.
My current code:
package Snomed.Snomed;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import catalog.Root;
public class Snomedinfo {
public void snomedinfoinsert()
{
Root oRoot = null;
ResultSet oRsSelect = null;
PreparedStatement oPrStmt = null;
PreparedStatement oPrStmt2 = null;
PreparedStatement oPrStmtSelect = null;
String strSql = null;
String strSql2 = null;
String snomedcode=null;
ResultSet oRs = null;
String refid = null;
String id = null;
String effectivetime = null;
String active = null;
String moduleid = null;
String conceptid = null;
String languagecode = null;
String typeid = null;
String term = null;
String caseSignificanceid = null;
int count = 0;
final int batchSize = 1000;
try{
oRoot = Root.createDbConnection(null);
strSql = "SELECT id FROM snomed_conceptdata WHERE active=1 ";
oPrStmt2 = oRoot.con.prepareStatement(strSql);
oRsSelect = oPrStmt2.executeQuery();
while (oRsSelect.next()) {
snomedcode = Root.TrimString(oRsSelect.getString("id"));
String sql = "INSERT INTO snomedinfo_data (refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid)SELECT refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid from snomed_descriptiondata WHERE conceptid =? AND active=1" ;
oPrStmtSelect = oRoot.con.prepareStatement(sql);
oPrStmtSelect.setString(1,snomedcode);
oPrStmtSelect.executeUpdate();
}
//oPrStmtSelect.executeBatch();
System.out.println("done");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
oRsSelect = Root.EcwCloseResultSet(oRsSelect);
oRs = Root.EcwCloseResultSet(oRs);
oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
oPrStmt = Root.EcwClosePreparedStatement(oPrStmt2);
oPrStmt = Root.EcwClosePreparedStatement(oPrStmtSelect);
oRoot = Root.closeDbConnection(null, oRoot);
}
}
public static void main(String args[] ) throws Exception
{
Snomedinfo a = new Snomedinfo();
a .snomedinfoinsert();
}
}
everything is working fine ie ,currently data(records) are getting inserted into the table 'snomedinfo_data' but after some time during insertion i suddenly get a java out of memory heap space error. please help!!
Form 1 is the textfield located
private void tblOrgMouseClicked(java.awt.event.MouseEvent evt) {
Connection cn = null;
Statement st = null;
ResultSet rss = null;
btnSave.setEnabled(false);
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);
try {
int row = tblOrg.getSelectedRow();
String cell_click = (tblOrg.getModel().getValueAt(row, 0).toString());
String sql = "SELECT * FROM tbl_organization WHERE org_id = '" + cell_click + "'";
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_organization?zeroDateTimeBehavior=convertToNull", "root", "");
st = cn.prepareStatement(sql);
rss = st.executeQuery(sql);
if (rss.next()) {
String addid = rss.getString("org_id");
txtOrgID.setText(addid);
String addname = rss.getString("org_name");
txtOrgName.setText(addname);
String adddesc = rss.getString("org_description");
txtOrgDesc.setText(adddesc);
String addadviser = rss.getString("org_adviser");
txtAdviserName.setText(addadviser);
}
} catch (Exception e) {
}
}
Form 2 is the Jtable
private void tblAdviserList2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Connection cn = null;
Statement st = null;
ResultSet rss = null;
String ab = " ";
try {
int row = tblAdviserList2.getSelectedRow();
String cell_click = (tblAdviserList2.getModel().getValueAt(row, 0).toString());
String sql = "SELECT * FROM tbl_adviser WHERE adviser_id = '" + cell_click + "'";
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_organization?zeroDateTimeBehavior=convertToNull", "root", "");
st = cn.prepareStatement(sql);
rss = st.executeQuery(sql);
if (rss.next()) {
String addid = rss.getString("firstname").concat(ab).concat(rss.getString("middlename")).concat(ab).concat(rss.getString("lastname"));
new FrmOrganization(addid);
this.setVisible(false);
}
} catch (Exception e) {
}
}
To get the value from a particular cell:
Object cellValue = table.getValueAt(row, col);
Alternatively, you can create a TableModel where each row represents a person object, and add a method on that.
hi my program work in back ground and show different message from database
in specific time the problem always show the same row
//////
**upload code **
public void myMethod(){
long startTime = System.currentTimeMillis();
int counter = 0;
while(true) {
if((System.currentTimeMillis() - startTime ) == 5000){
try{
String host = "jdbc:derby://localhost:1527/Quet";
String uName = "eenas";
String uPass= "2234";
con = DriverManager.getConnection( host, uName, uPass);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery( "SELECT * from EENAS.EENAS");
rs.next();
int id_col = rs.getInt("ID");
String id =Integer.toString(id_col);
String me=rs.getString("MESSAGE");
System.out.print(me);
rs.close();
}
catch(SQLException err)
{
JOptionPane.showMessageDialog(null, err);
}
startTime = System.currentTimeMillis();
continue; }
else{ continue; } } }
upload code
the can solve this problem by using arrayList to save all data, your program now show only that last one because they change their value every time, the solution like this,
public void DoConnect( ) {
try{
String host = "jdbc:derby://localhost:1527/Quet";
String uName = "eenas";
String uPass= "2234";
con = DriverManager.getConnection( host, uName, uPass);
stmt = con.createStatement();
rs = stmt.executeQuery("select * from EENAS.EENAS");
List<String> PMessage = new ArrayList<String>();
List<Integer> id_col = new ArrayList<Integer>();
while(rs.next()){
id_col.add(rs.getInt("ID"));
PMessage.add(rs.getString("MESSAGE"));
jTextArea1.setText(PMessage);
}
rs.close();
stmt.close();
con.close();
}
catch(SQLException err)
{
JOptionPane.showMessageDialog(null, err);
}
}
I didn't run the code on my machine but this is the logic can solve the problem.
I have a JFrame that has 3 JTextfields and 2 JDatechooser, what I am trying to do is if only one JTextfield has something typed in it and I press the search button, then I will be able to retrieve the data to JTable, but the problem is I have to fill out all JTextFileds and JDatechooser in order to retrieve data. My idea is to ignore null JTextfields and JTdatechooser if only one JTextfield has the keyword I want ?? Any suggestions ?? Thanks in advance,
public ArrayList<BillsRecord> getBillRecordByID(int EmpCode, String Fname, String Lname, String sDate, String eDate) throws SQLException {
String sql = "SELECT B.DATE AS DT, B.EMP_ID, E.FNAME, E.LNAME, MONEY_SENT, RENT, PHONE, GAS, ELECTRICITY, INTERNET, OTHER"
+ " FROM EMPLOYEE E INNER JOIN BILLS B ON E.EMP_ID = B.EMP_ID"
+ " WHERE B.EMP_ID = ? "
+ " OR E.FNAME = ? "
+ " OR E.LNAME = ? "
+ " OR DATE BETWEEN ? AND ? "
+ " ORDER BY B.DATE";
DBConnection con = new DBConnection();
Connection connect = con.getConnection();
PreparedStatement ps = null;
ArrayList<BillsRecord> records = new ArrayList<>();
try {
ps = connect.prepareStatement(sql);
ps.setInt(1, EmpCode);
ps.setString(2, Fname);
ps.setString(3, Lname);
ps.setString(4, sDate);
ps.setString(5, eDate);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
BillsRecord billrec = new BillsRecord();
billrec.setDATE(rs.getString("DT"));
billrec.setEMP_ID(rs.getInt("EMP_ID"));
billrec.setFNAME(rs.getString("FNAME"));
billrec.setLNAME(rs.getString("LNAME"));
billrec.setMONEY_SENT(rs.getDouble("MONEY_SENT"));
billrec.setRENT(rs.getDouble("RENT"));
billrec.setPHONE(rs.getDouble("PHONE"));
billrec.setGAS(rs.getDouble("GAS"));
billrec.setELECTRICITY(rs.getDouble("ELECTRICITY"));
billrec.setINTERNET(rs.getDouble("INTERNET"));
billrec.setOTHER(rs.getDouble("OTHER"));
records.add(billrec);
return records;
}
} catch (SQLException e) {
System.out.println(e.toString());
} finally {
if (ps != null) {
ps.close();
}
if (connect != null) {
connect.close();
}
}
return null;
}
private void search() {
try {
JTextField stxt = ((JTextField) startdatetxt.getDateEditor().getUiComponent());
String sDATE = stxt.getText().trim();
JTextField etxt = ((JTextField) enddatetxt.getDateEditor().getUiComponent());
String eDATE = etxt.getText().trim();
int EMP_ID = Integer.parseInt(this.empidtxt.getText().trim());
String FNAME = this.firstnametxt.getText().trim();
String LNAME = this.lastnametxt.getText().trim();
BillRecordDao billrecdao = new BillRecordDao();
ArrayList<BillsRecord> records = billrecdao.getBillRecordByID(EMP_ID, FNAME, LNAME, sDATE, eDATE);
Object[] tableColumnName = new Object[11];
tableColumnName[0] = "Date";
tableColumnName[1] = "H.License";
tableColumnName[2] = "First Name";
tableColumnName[3] = "Last Name";
tableColumnName[4] = "MONEY SENT";
tableColumnName[5] = "RENT";
tableColumnName[6] = "PHONE";
tableColumnName[7] = "GASE";
tableColumnName[8] = "ELECTRICITY";
tableColumnName[9] = "INTERNET";
tableColumnName[10] = "OTHER";
DefaultTableModel tbd = new DefaultTableModel();
tbd.setColumnIdentifiers(tableColumnName);
this.BillsSummaryTable.setModel(tbd);
Object[] RowRec = new Object[11];
for (int i = 0; i < records.size(); i++) {
RowRec[0] = records.get(i).getDATE();
RowRec[1] = records.get(i).getEMP_ID();
RowRec[2] = records.get(i).getFNAME().toUpperCase();
RowRec[3] = records.get(i).getLNAME().toUpperCase();
RowRec[4] = records.get(i).getMONEY_SENT();
RowRec[5] = records.get(i).getRENT();
RowRec[6] = records.get(i).getPHONE();
RowRec[7] = records.get(i).getGAS();
RowRec[8] = records.get(i).getELECTRICITY();
RowRec[9] = records.get(i).getINTERNET();
RowRec[10] = records.get(i).getOTHER();
tbd.addRow(RowRec);
}
} catch (SQLException e) {
System.out.println(e.toString());
}
}
Basically, you need to create a variable/dynamic query based on the available values
Now, you can do this using something like StringBuilder or even storing each query element in a List or array, but you always end up with the "trailing OR" problem (you need to know when you've got to the last element and not append the "OR" to the String or remove the trailing "OR" from the resulting String). While not difficult, it's just a pain.
However, if you're using Java 8, you can use StringJoiner!
StringJoiner sj = new StringJoiner(" OR ");
String sql = "SELECT B.DATE AS DT, B.EMP_ID, E.FNAME, E.LNAME, MONEY_SENT, RENT, PHONE, GAS, ELECTRICITY, INTERNET, OTHER"
+ " FROM EMPLOYEE E INNER JOIN BILLS B ON E.EMP_ID = B.EMP_ID"
+ " WHERE ";
List values = new ArrayList();
// EmpCode MUST be a Integer, so it can be null
if (EmpCode != null) {
sj.add("B.EMP_ID = ?");
values.add(EmpCode);
}
if (FName != null) {
sj.add("E.FNAME = ?");
values.add(FName);
}
if (LName != null) {
sj.add("E.LNAME = ?");
values.add(LName);
}
if (sDate != null && eDate != null) {
sj.add("DATE BETWEEN ? AND ?");
values.add(sDate);
values.add(eDate);
}
sql += sj.toString();
Connection connect = null;
try (PreparedStatement ps = connect.prepareStatement(sql)) {
for (int index = 0; index < values.size(); index++) {
ps.setObject(index + 1, values.get(index));
}
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
//...
}
}
} catch (SQLException exp) {
exp.printStackTrace();
}
You might also like to have a look at The try-with-resources Statement and have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
I have a database with all the values stored in a table. I want to print a name from the table using drawString() method. I hv created a resultset and the arraylist. Is there any way to pass a string from database in drawString method??
Please provide me a code or link to a code as I am new to this..Thankyou.
public class AB1 extends JPanel
{ public static void main(String a[]) throws Exception
{
int ID,TC;
String FROM, TO, ATS, DTS, SOURCE, DSTN, TS, ST;
ResultSet rs;
ArrayList<AB> ab = new ArrayList<AB>();
//DATABASE CONNECTION
String TrainNo = "287972";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#127.0.0.1:1521:xe";
Connection c = DriverManager.getConnection(url, "system", "mypcdatabase");
// STORING TABLE IN RESULTSET
int rows = 0;
PreparedStatement st=c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?" );
rs=st.executeQuery();
PreparedStatement st1=c.prepareStatement("select * from MT_RAKE_TRAIN_LINK_MASTER where TRAIN_NUMBER = ?");
rs = st1.executeQuery();
PreparedStatement st2=c.prepareStatement("select * from MT_SLIP_TRAIN_INFO where TRAIN_NUMBER = ?");
rs = st2.executeQuery();
while(rs.next())
{
ID = rs.getInt("ID_TRAIN_DEF");
FROM = rs.getString("COACH_FROM");
TO = rs.getString("COACH_TO");
TC = rs.getInt("NUMBER_OF_COACHES");
ATS = rs.getString("ATTACH_TRANSFER_STATION");
DTS = rs.getString("DETACH_TRANSFER_STATION");
SOURCE = rs.getString("TRAIN_SRC");
DSTN = rs.getString("TRAIN_DSTN");
TS = rs.getString("TRANSFER_STATION");
ST = rs.getString("slip_type");
AB obj = new AB();
obj.id = ID;
obj.tc = TC;
obj.from = FROM;
obj.to = TO;
obj.ats = ATS;
obj.dts = DTS;
obj.src = SOURCE;
obj.dstn = DSTN;
obj.ts = TS;
obj.st = ST;
ab.add(obj);
rows++;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
getImg(new FileOutputStream("C:\\A&B" + TrainNo + ".jpg"), ab);
}
public static void getImg(OutputStream out, ArrayList<AB> ab) throws IOException
{ int imgWidth = 1024, imgHeight = 768;
BufferedImage image = new BufferedImage(imgWidth,imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gx = image.createGraphics();
gx.setColor(Color.BLUE);
gx.setColor(Color.BLACK);
gx.drawLine(60,350,970,350);
gx.drawLine(970,350,960,340);
gx.drawLine(970, 350, 960, 360);
gx.drawString("", 30 , 330);
gx.drawString("", 960 , 330);
ImageIO.write(image, "PNG", out);
out.close();
}
}
In your these lines
PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
rs=st.executeQuery();
You forgot to provide any value for the ?, something like
PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
st.setString(1, TrainNo);
rs=st.executeQuery();
Please Learn Java Naming Conventions and stick to them.
# gagandeep bali
the line is correct
String TrainNo = "287972";
this value is passed in place of the ?