I made a Java application on netbeans about ranking the vikor method,
but when going to save the table data to the database, an error appears saying that the count table is mismatch.
I have tried changing the syntax and database settings. But still
Is there anything I missed?
Anything about storing data from table calculations directly to the database?
This is my code:
public void cariperingkat(){
try {
LinkedList mm=new LinkedList();
ResultSet res3 = conn.ambilData("select * from bobot");
tabelmodelPERINGKAT();
while (res3.next()){
mm.add(res3.getString(1));
mm.add(res3.getString(2));
mm.add(res3.getString(3));
mm.add(res3.getString(4));
}
for (int t = 0; t < jTable2.getRowCount(); t++) {
String sql = "delete from data_alternatif WHERE nama= nama";
conn.simpanData(sql);
}
for (int x = 0; x < jTable2.getRowCount(); x++) {
double r1;
double r2;
double r3;
double r4;
double w;
r1=(Float.valueOf(jTable2.getValueAt(x, 1).toString())*Float.valueOf(mm.get(0).toString()));
r2=(Float.valueOf(jTable2.getValueAt(x, 2).toString())*Float.valueOf(mm.get(1).toString()));
r3=(Float.valueOf(jTable2.getValueAt(x, 3).toString())*Float.valueOf(mm.get(2).toString()));
r4=(Float.valueOf(jTable2.getValueAt(x, 4).toString())*Float.valueOf(mm.get(3).toString()));
w=r1+r2+r3+r4;
String sql = "insert into data_alternatif values"+
"('"+jTable2.getValueAt(x, 0).toString()+"', "+
"'"+w+"')";
conn.simpanData(sql);
}
tabelmodelPERINGKAT();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, ex);
}
}
Related
I have some codes for doing my project. There is a access database that have some tables and records. I'm taking this records from access database and I'm creating MS Word document with them. My codes like this
public static void partoff(String SalesOrder)
{
try {
ResultSet deneme4,deneme5,deneme6,deneme,deneme0,deneme10,deneme11 = null;
deneme10=sorgu.executeQuery("SELECT GroupName,ID From TableRun Where LID IN (Select DISTINCT strLID from TableResult Where strSO='"+SalesOrder+"') ORDER BY ID");
ArrayList all_groups=new ArrayList();
while(deneme10.next()) {
all_groups.add(deneme10.getString("GroupName"));
//System.out.println(all_groups.size());
}
ArrayList LIDs=new ArrayList();
deneme11=sorgu.executeQuery("SELECT LID,ID FROM TableRun WHERE GroupName IN (SELECT GroupName,ID From TableRun Where LID IN (Select DISTINCT strLID from TableResult Where strSO='"+SalesOrder+"')) ORDER BY ID");
while(deneme11.next())
{
LIDs.add(deneme11.getString("LID"));
System.out.println(LIDs.size());
}
for(int qq=0; qq<all_groups.size(); qq++) {
deneme4=sorgu.executeQuery("SELECT * FROM TableResult WHERE strSO='" + SalesOrder + "' AND strLID='"+LIDs.get(sayac)+"'");
deneme5=sorgu.executeQuery("SELECT * FROM TablePARTON WHERE strSO='" + SalesOrder + "' AND strLID='"+LIDs.get(sayac)+"'");
deneme6=sorgu.executeQuery("SELECT * FROM TableIW59 WHERE strSO='" + SalesOrder + "' AND strLID='"+LIDs.get(sayac)+"'");
while(deneme4.next()) {
System.out.println(off[satir1][sayac2+8]=deneme4.getString("DefectDes"));
if(!off[satir1][sayac2+8].equalsIgnoreCase("Not Disassembled"))
{
off[satir1][sayac2]=deneme4.getString("strTASK");
off[satir1][sayac2+1]=deneme4.getString("strLID");
off[satir1][sayac2+2]=deneme4.getString("strPN");
off[satir1][sayac2+3]=deneme4.getString("intQPE");
off[satir1][sayac2+4]=deneme4.getString("intSV");
off[satir1][sayac2+5]=deneme4.getString("intIS");
off[satir1][sayac2+6]=deneme4.getString("intRS");
off[satir1][sayac2+7]=deneme4.getString("intREP");
off[satir1][sayac2+8]=deneme4.getString("DefectDes");
satir1++;
}
else {
//System.out.println(deneme.getString("GroupName"));
//satir1=0;
//deneme4.next();
}
}
while(deneme5.next())
{
onn[satir2][sayac2]=deneme5.getString("strPN");
onn[satir2][sayac2+1]=deneme5.getString("intNewOnQty");
onn[satir2][sayac2+2]=deneme5.getString("intRepOnQty");
onn[satir2][sayac2+3]=deneme5.getString("intSVOnQty");
onn[satir2][sayac2+4]=deneme5.getString("strRemark");
satir2++;
}
while(deneme6.next())
{
if(!deneme6.getString("strNotifType").equals("ER")) {
dispon[satir3][sayac2]=deneme6.getString("strNotifType");
dispon[satir3][sayac2+1]=deneme6.getString("strPN");
dispon[satir3][sayac2+2]=deneme6.getString("strQty");
dispon[satir3][sayac2+3]=deneme6.getString("strCodeText");
dispon[satir3][sayac2+4]=deneme6.getString("strGroupText");
dispon[satir3][sayac2+5]=deneme6.getString("strShortText");
dispon[satir3][sayac2+6]=deneme6.getString("strLongText");
satir3++;
}
}
if(satir3>0) {
int x=0;
int y=0;
int q=0;
for(int i=0; i<satir3; i++) {
j=i+1;
while(j<=satir3) {
if(dispon[i][1].equals(dispon[j][1]) && dispon[i][3].equals(dispon[j][3])&& dispon[i][4].equals(dispon[j][4])){
x = Integer.parseInt(dispon[i][2]);
y = Integer.parseInt(dispon[j][2]);
q = x+y;
dispon[i][2]=Integer.toString(q);
dispon[j][0]="boss";
}
j++;
}
}
}
disponsatir=0;
for(int i = 0; i < satir3; i++)
{
for(int j = 0; j<7 ;j++)
{
if(!dispon[i][0].equals("boss"))
{
dispon_new[disponsatir][j]=dispon[i][j];
if(j==6) disponsatir++;
}
}
}
sayac++;
if(!all_groups.get(qq).equals(all_groups.get(qq+1))) {
create_table1(satir1,all_groups.get(qq).toString());
sayac3=0;
sayac2=0;
satir1=0;
satir2=0;
satir3=0;
}
}
System.out.println("Success");
} catch (Exception e) {
// TODO: handle exception
System.out.println("NOT Successfully");
System.out.println(e.getLocalizedMessage());
}
}
It was working nicely yesterday. I changed something in codes but I don't remember what is it. After then I tried to run my codes, I'm getting this error.
UCAExc:::3.0.7 row column count mismatch
Why I'm getting this error?
I am stuck in a problem. When I try to send data from jtable to sql database through stored procedure. here what I am doing:
inserting data to jtable
String b= jLabel116.getText(),c=jTextField6.getText(),e=jTextField20.getText(),f=jTextField25.getText(),g=jTextField48.getText();
float x,y,z;
x=Float.parseFloat(jTextField25.getText());
y=Float.parseFloat(jTextField48.getText());
z=x*y;
String total1=String.valueOf(z);
DefaultTableModel df = (DefaultTableModel) jTable5.getModel();
df.addRow(new Object[]{b,c,d,f,g,total1});
int rowsCount = jTable5.getRowCount();
int Price = 0,Qty=0, total=0;
for(int i=0;i<rowsCount;i++){
Price += Integer.parseInt(jTable5.getValueAt(i,3).toString());
Qty += Integer.parseInt(jTable5.getValueAt(i,4).toString());
}
total = Price*Qty;
System.out.println(total);
jTextField26.setText(String.valueOf(total));
jTextField51.setText(String.valueOf(total));
jTextField50.setText(String.valueOf(Qty));
jTable5.setModel(df);
Sending Data to Database
try{
DefaultTableModel df = new DefaultTableModel();
df=(DefaultTableModel) jTable5.getModel();
CallableStatement cs=m.XC.prepareCall("call Prod_SALE (?,?,?,?,?,?,?,?)");
for (int i = 0; i < df.getRowCount(); i++) {
for (int j = 0; j < df.getColumnCount(); j++) {
Object o = df.getValueAt(i, j);
System.out.println("object from table is : " +o);
cs.setString(j+1, (String)o);
cs.addBatch();
}
cs.executeBatch();
cs.clearParameters();
}
}
catch(Exception ex){
ex.printStackTrace();
Error Exception:
java.sql.SQLException: Parameter-Set has missing values.
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.addBatch(JdbcOdbcPreparedStatement.java:1546)
Please help me....I am unable to solve it
You call addBatch in the inner loop (variable j) after you have set one parameter. Obviously this fails since you have 8 parameters. The Javadoc of PreparedStatement.addBatch says:
Adds a set of parameters to this PreparedStatement object's batch of
commands.
You need to move the call to addBatch out of the inner loop.
(And probably the executeBatch should be moved out of the outer loop (variable i) too.
DefaultTableModel df = (DefaultTableModel) jTable5.getModel();
CallableStatement cs=m.XC.prepareCall("call Prod_SALE (?,?,?,?,?,?,?,?)");
for (int i = 0; i < df.getRowCount(); i++)
{
for (int j = 0; j < df.getColumnCount(); j++)
{
Object o = df.getValueAt(i, j);
System.out.println("object from table is : " +o);
cs.setString(j+1, (String)o);
}
cs.addBatch();
}
cs.executeBatch();
As the message says, you have not set all values in your SQL.
I have a strange error which I can not get my head around where the .size() method does not appear to return the correct value.
This first bit of code creates the ArrayList.
public void createResultList(String query) {
ArrayList<ArrayList<String>> data = new ArrayList();
try {
ResultSet rs = st.executeQuery(query);
ResultSetMetaData meta = rs.getMetaData();
for(int i = 0; i < meta.getColumnCount(); i++) {
data.add(i, new ArrayList<String>());
}
int x = 0;
while(rs.next()) {
for(int y = 0; y < meta.getColumnCount(); y++) {
data.get(x).add(rs.getString(y + 1));
}
x++;
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
ResultTable result = new ResultTable(data);
JTable table = new JTable(result);
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
refresh();
}
This is my TableModel class which is used to create the table when it's passed to it.
public class ResultTable extends AbstractTableModel {
private ArrayList<ArrayList<String>> data;
public ResultTable(ArrayList<ArrayList<String>> data) {
this.data = data;
}
public int getColumnCount() {
return data.get(0).size();
}
public int getRowCount() {
return data.size();
}
public Object getValueAt(int row, int col) {
return data.get(row).get(col);
}
}
Now the the problem is in the ResultTable class, now for a select query returning one row with 12 columns, the first data.get(0).size() call correctly returns 12, but the 2nd data.size() call incorrectly returns 12 also instead of 1, this is causing out of bounds errors, can anyone please explain this seemingly paradoxical result?
This is something you should've found easily when you debug your code...
public void createResultList(String query) {
ArrayList<ArrayList<String>> data = new ArrayList();
data is an ArrayList of ArrayLists
try {
ResultSet rs = st.executeQuery(query);
A query returning 1 row of 12 columns
ResultSetMetaData meta = rs.getMetaData();
for(int i = 0; i < meta.getColumnCount(); i++) {
For each column in your recordset, being 12, you add an empty ArrayList in data
data.add(i, new ArrayList<String>());
}
resulting in data being an ArrayList of 12 empty Arraylists
This already explains why data.size() == 12
int x = 0;
while(rs.next()) {
for each record in your recordset, being 1
for(int y = 0; y < meta.getColumnCount(); y++) {
for each column in your recordset, being 12, you add a string to the ArrayList with the same index as the recordset
data.get(x).add(rs.getString(y + 1));
}
The first ArrayList in data (data.get(0)) will have 12 Strings
All other ArrayLists in data (data.get(x) where x > 0) will remain empty
x++;
}
Resulting in data being an ArrayList of 12 ArrayLists
of which only the first ArrayList has 12 Strings and the others are empty
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
ResultTable result = new ResultTable(data);
JTable table = new JTable(result);
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);
refresh();
}
When you create the data list, you create a sublist to hold the data for each column.
If that is what you want to do, you should add data from each column of the result set to the list that corresponds to it:
while(rs.next()) {
for(int y = 0; y < meta.getColumnCount(); y++) {
data.get(y).add(rs.getString(y + 1));
}
}
Okay. Here's a piece of code that should return an array after searching a database
with JDBC. It's the Apache Derby database that's built into the JDK.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class readnames {
static Object [] name;
static Object [] type;
static Object [] desc;
static ArrayList<String> l = new ArrayList<String>();
static ArrayList<String> l2 = new ArrayList<String>();
static ArrayList<String> l3 = new ArrayList<String>();
public static Object[] lies(String desq) {
final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
final String CONNECTION = "jdbc:derby:AccountDatabase";
try {
Class.forName(DRIVER).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection connection = DriverManager.getConnection(CONNECTION);
PreparedStatement statement = connection.prepareStatement("select * from WARPS WHERE CATEGORY=?");
statement.setString(1, "Server");
ResultSet resultset = statement.executeQuery();
if (resultset.next()) {
while(resultset.next()) {
l.add(resultset.getString("NAME"));
l2.add(resultset.getString("CATEGORY"));
l3.add(resultset.getString("DESCRIP"));
}
name = l.toArray();
type = l2.toArray();
desc = l3.toArray();
for (int i = 0; i < name.length; i++){
System.out.println(name[i]);
}
for (int i2= 0; i2 < type.length; i2++) {
System.out.println(type[i2]);
}
for (int i3= 0; i3 < desc.length; i3++) {
System.out.println(desc[i3]);
}
System.out.println("The method is called");
} else {System.out.println("No entries exist in this category");
}
} catch (SQLException e) {
e.printStackTrace();
}
return name;
}
}
My problem is, it doesn't return anything at all. It worked fine until I tried filtering the data using a PreparedStatement with a WHERE-clause in it. But I really don't want to get ALL the data, but specific data. The calls to println were added for debug purposes; nothing is printed when the where-clause is in the code.
EDIT: The l2.add and l3.add clauses in the while-loop are also just for debug purposes. Sry.
btw, the data that I want to retrieve definetely is in the database and gets printed when I delete the where-clause.
Does anyone know what causes this and how to fix it?
If your result just has 1 row, you are going to not get anything.
The reason:
if (resultset.next()) {
while(resultset.next()) {
When you do a .next() in the `while, the cursor has already been advanced once by if.
You should just be doing
while(resultset.next()) {
this does not execute if there are no rows.
try this, don't use if and while, try to use if and do while;
if (resultset.next()) {
do {
l.add(resultset.getString("NAME"));
l2.add(resultset.getString("CATEGORY"));
l3.add(resultset.getString("DESCRIP"));
}while(resultset.next());
name = l.toArray();
type = l2.toArray();
desc = l3.toArray();
for (int i = 0; i < name.length; i++){
System.out.println(name[i]);
}
for (int i2= 0; i2 < type.length; i2++) {
System.out.println(type[i2]);
}
for (int i3= 0; i3 < desc.length; i3++) {
System.out.println(desc[i3]);
}
System.out.println("The method is called");
} else {System.out.println("No entries exist in this category");}
and in query write.
Connection connection = DriverManager.getConnection(CONNECTION);
PreparedStatement statement = connection.prepareStatement("select * from WARPS WHERE lower(CATEGORY)=?");
statement.setString(1, "Server".toLowerCase());
don't forget to use string functions which using where clause.
I suspect that your query is returning exactly one row but you're skipping it. This line:
if (resultset.next()) {
advances the ResultSet to the first row. But then your next statement
while(resultset.next()) {
advances it to the next row, which probably does not exist.
You should have only the while loop; remove the if. You can set a boolean variable to true within the while loop so that you can tell if you had records and later print "No entries exist in this category" if there weren't any rows.
Why do you surround your while with an if statement? The condition moves the resultset one row forward so you're missing the first row. If there aren't any rows left the while statement will exit by itself. The if surrounding it is not required.
I have an applet that I am trying to make read a file. It throws an exception, but I am passing it the correct path so I am not sure where I am going wrong. I am using this to read numbers and use those numbers to change a multidimensional array, if you were wondering. Heres the code:
public class Save {
public void loadSave(File loadPath) {
try {
Scanner loadScanner = new Scanner(loadPath);
while(loadScanner.hasNext()){
for(int y = 0; y < Screen.room.block.length;y++){
for(int x = 0; x < Screen.room.block[0].length;x++){
Screen.room.block[y][x].groundID = loadScanner.nextInt();
System.out.println(loadScanner.nextInt());
}
}
for(int y = 0; y < Screen.room.block.length;y++){
for(int x = 0; x < Screen.room.block[0].length;x++){
Screen.room.block[y][x].airID = loadScanner.nextInt();
}
}
}
loadScanner.close();
} catch (Exception e) { e.printStackTrace();}
}
}
How I access it:
save.loadSave(new File(frame.getClass().getResource("mission1.tdm").toString()));
Ok, I used the edited code up above and it still says that it cannot find the file, even though the error spits out the exact path that it is in.