try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
st3 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery("select insp_no,ro_code, inspectiondate from keroseneheader where ro_code='" + cust_code + "' and status='NO' and inspectedby='" + em.ro_code + "'");
if (rs.next())
{
session.setAttribute("insp_no", rs.getString("insp_no"));
out.print(rs.getString("insp_no") + "#" + rs.getString("inspectiondate"));
}
else
{
rs3 = st3.executeQuery("select * from keroseneheader where ro_code='" + cust_code + "' and inspectiondate>to_date('" + inspectdate + "','dd-mm-yyyy') and inspectedby='" + em.ro_code + "'");
if (!rs3.next())
{
String query2 = "insert into keroseneheader(insp_no,ro_code,inspectiondate,status,inspectedby) values((select nvl(max(INSP_NO)+1,1) from keroseneheader),'" + cust_code + "',"
+ "to_date('" + inspectdate + "','dd-mm-yyyy'),'NO','" + em.ro_code + "')";
boolean i = DbConn.insertupdatedata(query2, false);
if (i) {
st1 = con.createStatement();
rs1 = st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') inspectiondate from keroseneheader where ro_code=" + cust_code + " and status='NO' and inspectedby='" + em.ro_code + "'");
if (rs1.next()) {
session.setAttribute("insp_no", rs1.getString("insp_no"));
out.print(rs1.getString("insp_no") + "#" + rs.getString("inspectiondate"));
}
}
}
else {
out.print("Invalid Date");
}
}
Change this line
st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') inspectiondate from keroseneheader
To this
st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') as 'inspectiondate' from keroseneheader
Related
I have the following code, but UCanAccess does not allow me to use IF NOT EXISTS. Does anyone have any ideas?
"if not exists (select ArtikelNr from Artikel where ArtikelNr='"+ValueRead.prüfen1()+"')" +
"begin" +
" if not exists (select isa_id from Artikel where isa_id='"+Integer.parseInt(Data.textField.getText())+"')" +
" begin" +
"INSERT INTO Artikel ([HERE ARE THE TABLES) VALUES
[HERE ARE THE VALUES]
" end" +
"end";
You could do something like this:
// test data
int artikelNr = 123;
int isa_id = 1234;
String description = "artikel_123";
String sql = ""
+ "INSERT INTO Artikel (ArtikelNr, isa_id, description) "
+ "SELECT ? AS ArtikelNr, ? AS isa_id, ? as description "
+ "FROM DUAL "
+ "WHERE NOT EXISTS ("
+ " SELECT * FROM Artikel "
+ " WHERE ArtikelNr=? AND isa_id=?)";
PreparedStatement ps = conn.prepareStatement(sql);
// values to INSERT
ps.setInt(1, artikelNr);
ps.setInt(2, isa_id);
ps.setString(3, description);
// values for WHERE NOT EXISTS
ps.setInt(4, artikelNr);
ps.setInt(5, isa_id);
int rtn = ps.executeUpdate();
System.out.printf("%d row(s) inserted%n", rtn);
I have that query that shows all my Boats, but I would just like to show the active boats.
public Pagination<Boats> GetAll(string name , int pageSize, int pageNumber)
{
var cn = Db.Database.Connection;
var sql = #"SELECT * FROM Boats" +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%')" +
"ORDER BY [Name ] " +
"OFFSET " + pageSize * (pageNumber - 1) + " ROWS " +
"FETCH NEXT " + pageSize + " ROWS ONLY " +
" " +
"SELECT COUNT(Id) FROM Boats" +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%')";
var multi = cn.QueryMultiple(sql, new { Name = name });
var boats= multi.Read<Boats >();
var total = multi.Read<int>().FirstOrDefault();
var paginationList= new Pagination<Boats>()
{
List = boats,
Count = total
};
return paginationList;
}
I have the active and excluded attributes in the table, so I tried to show only the boats that were active, but with this query, it keeps returning all records from the boat table, like frist one
var sql = #"SELECT * FROM Boats " +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%' AND Active=1 AND Excluded=0)" +
"ORDER BY [Name] " +
"OFFSET " + pageSize * (pageNumber - 1) + " ROWS " +
"FETCH NEXT " + pageSize + " ROWS ONLY " +
" " +
"SELECT COUNT(Id) FROM Boats " +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%' AND Active=1 AND Excluded=0)";
Any Ideas how can I combine the Name attribute with the Active and Excluded attributes?
Remove it from the parenthesis:
"WHERE (#Name IS NULL OR Name LIKE #Name + '%') AND Active=1 AND Excluded=0"
I use below query in my jsp page. but I'm not sure Is it true to use this query in jsp page.
int i = st.executeUpdate("'BEGIN TRANSACTION DECLARE #id [int] SELECT #id = SCOPE_IDENTITY() INSERT INTO Viewer(Reserve_ID, F_Name, L_Name, Competition_ID, City, Phone, [E-mail]) VALUES (#id, '" + fname + "','" + lname + "','" + 30 + "','" + city + "','" + phone + "','" + email + "' ) INSERT INTO Reservation_Inf(Reservation_Date, Competition_ID, NumberOfTicket, Position_ID) VALUES ('" + dNow + "','" + 30 + "','" + 1 + "','" + 8 + "' ) COMMIT TRANSACTION '" );
if (i > 0) {
response.sendRedirect("Success.jsp");
} else {
response.sendRedirect("Fail.jsp");
}
It gives this error :
Incorrect syntax near 'BEGIN TRANSACTION DECLARE #id [int] SELECT #id = SCOPE_IDENTITY() INSERT INTO Viewer(Reserve_ID, F_Name, L_Name, Competition_ID,'.
the UPDATE gives ???? if the updater field was written in Arabic and this is my query:
UPDATE students
SET first_name = 'الاسم' , last_name = 'الاسم الاخير' ,
father_name = 'الاسم الاخير' , mother_name = '',
birth_date = '1/1/1990 12:00:00 AM' , education_level = '' ,
address = '' , notes = ''
WHERE student_id = 33
And here is the result of the update:
student_id first_name last_name mother_name father_name birth_date
33 ????? ????? ?????? ??????????? 1990-01-01
//the answer is great and thank you people, another question is that I am using this UPDATE syntax in my C# program
command.CommandText = "UPDATE students SET " +
"first_name = " + "'" + first_name + "'" + " , last_name = " + "'" + last_name + "'" +
" , father_name = " + "'" + father_name + "'" + " , mother_name = " +
"'" + mother_name + "'" + ", birth_date = " + "'" + birth_date + "'" +
" , education_level = " + "'" + education_level + "'" +
" , address = " + "'" + address + "'" + " , notes = " + "'" + notes + "'" +
" WHERE student_id = " + id ;
//how to use the character N
You have forgotten the N prefix before your string literals which is required so they will be treated as nvarchar rather than varchar
SET first_name = N'الاسم' etc.
without that the text is coerced into whatever characters the code page of your default collation can deal with.
Create the database with this collation Arabic_CI_AS, you won't need to put N before the Arabic characters.
public void ModificaNomeUtente(int slipno , int basicprice , int premium, int totalamountpaid, int weight , int totalamountbasic , int totalamountpremium , int yeildestimates , int farmercode)
{
// Creo la stringa di Connessione al DataBase
SqlConnection sqlConn = new SqlConnection(#"Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");
// Prova
try
{
// Creo la query che andrà a modificare il nome utente
string sqlQuery = "UPDATE cottonpurchse SET slipno = '" + slipno + "' + basic price = '" + basicprice + "' + premium = '" + premium + "' + totalamountpaid = '" + totalamountpaid + "' + weight = '" + weight + "' + totalamountbasic = '" + totalamountbasic + "' + totalamountpremium = '" + totalamountpremium + "' + yeildestimated = '" + yeildestimates + "' = WHERE farmercode = '" + FarmerCode + "'";
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
// Apro la connessione con il DataBase Login.sdf
sqlConn.Open();
// Eseguo l'istruzione sql
cmd.ExecuteNonQuery();
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// Se la query ha avuto esito positivo imposto la variabile result a true
result = true;
}
// In caso di eccezzione
catch (Exception ex)
{
// Visualizzo il messaggio con la relativa eccezzione verificatasi
MessageBox.Show(ex.Message);
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
// la query ha avuto esito negativo imposto la variabile result a false
result = false;
}
finally
{
// Chiudo la connessione con il DataBase Login.sdf
sqlConn.Close();
}
}
this doesnt work its not updating could it be cause my values are set to null ?
That statement parses to something like;
UPDATE cottonpurchse
SET slipno = '???' + basic price = '???' + premium = '???' + totalamountpaid = '???' + weight = '???' + totalamountbasic = '???' + totalamountpremium = '???' + yeildestimated = '???' = WHERE farmercode = '???'
There is an = before the WHERE which is illegal, did you mean , instead of + ?
UPDATE cottonpurchse
SET slipno = '???' , basic price = '???' , premium = '???' , totalamountpaid = '???' , weight = '???' , totalamountbasic = '???' , totalamountpremium = '???' , yeildestimated = '???' WHERE farmercode = '???'
UPDATE: everyone's comments are valid. Your SQL is full of errors: extra spaces, extra +, and extra =. You are confusing yourself trying to build up a string that way...
That said, your SQL query string should be this:
string sqlQuery = "UPDATE cottonpurchse SET slipno=" + slipno + ", basicprice=" + basicprice + ", premium=" + premium + ", totalamountpaid=" + totalamountpaid + ", weight=" + weight + ", totalamountbasic=" + totalamountbasic + ", totalamountpremium=" + totalamountpremium + ", yeildestimated=" + yeildestimates + " WHERE farmercode=" + FarmerCode;
It is confusing becuase you are building a string. The best (and safest) way to do what you are trying to do is use SQL parameters. See: c# Sqlcommand error (specifically the answer that shows "Parameters.Add")