Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How can I insert jdatechooser from java to database? I have used all the solutions of the net but still I can't insert. Please help me.
I'm using eclipse environment.
This is my code:
try {
PreparedStatement stm= (PreparedStatement) con.prepareStatement("INSERT INTO d"+
"(dateEntré)"+
"VALUES(?)");
((PreparedStatement)stm).setDate(1,convertUtilDateToSqlDate(dateChooser.getDate()));
statement.execute();
JOptionPane.showMessageDialog(null,"added");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null,e1.getMessage());
}
Your code example seems to be wrong, and this could just be a typo, but since you've provided no other information with regards to the error, this is all we have to go on...
You create a PreapredStatement called stmt, but then use statement to execute the query.
You should be using the same variable/instance to bind and excute the query. You should probably also use executeUpdate instead of execute, this provides some additional information when the statement is executed about the number of rows that were affected by the update.
try {
PreparedStatement stm= (PreparedStatement) con.prepareStatement("INSERT INTO d (dateEntré) VALUES(?)");
stm.setDate(1,convertUtilDateToSqlDate(dateChooser.getDate()));
int rows = statement.executeUpdate();
if (rows > 0) { // Should be 1
JOptionPane.showMessageDialog(null,"added");
} else {
JOptionPane.showMessageDialog(null,"Update failed for unknown reason");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null,e1.getMessage());
e1.printStackTrace();
}
This of course assumes that the database column is a compatible type of java.sql.Date
Take a closer look at JDBC Database Access and Using Prepared Statements for more details.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to create a simple JDBC method to delete from my DB, and I'm not sure if I'm going about this the correct way. This is inside one of my services.
Method:
public void deleteLocation(Integer id) {
String DELETE = "DELETE FROM locale WHERE id=?";
namedParameterJdbcTemplate.update(DELETE, new BeanPropertySqlParameterSource(id));
}
I would try changing your update line to
namedParameterJdbcTemplate.update(DELETE, id);.
If you are using spring-boot. Then you should be using spring-data-jpa to manage your database. Jdbc is Hard way of doing this.
If you are using jdbc delete to a specific row use prepared statement. You can refer this:
preparedStatement = connection.prepareStatement("DELETE * from table_name WHERE id= ?");
preparedStatement.setInt(1, id);
return !preparedStatement.execute();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I wrote a SQL query in my hibernate application using where clause and and .
Here is method ,
public List<Comobility> getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
Query query = session.createQuery("from Comobility where comobility_items_idcomobility_items= :idComobilityItems and patient_idpatient= :idpatinet");
query.setParameter("idComobilityItems", idComobilityItems);
query.setParameter("idpatinet", idPatient);
List<Comobility> list = query.list();
return list;
}
But this is not working . There is no any exception or error . Actually, there is no any result.
Have any ideas ?
You should try like that
Wrap your method with try catch block. I believe the query has an error then it will enter to catch block
public List<Comobility>getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
try {
logger.debug("xxxx")
//do comething
} catch (Exception e) {
e.printStackTrace();
}
}
Make sure your method parameters are not null
Check your list is empty or not.
For checking you can use IDE debugger or put some logs in your method.
You need to check the generated SQL in the log and execute it against database. If query is compiled and no records are returned, means there are no records for the particular criteria.
To generate the SQL by Hibernate, please set property show_sql to true in hibernate configuration.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem with searching a particular contact using part of a name. I know how it would look like in SQL but I cant implement it using Java.
if (rs.getString(nameTable LIKE '%name1%';)
Consider adding the LIKE clause to your SQL query instead of handling it in java code:
try(PreparedStatment ps = con.prepareStatement("SELECT * " +
" FROM Contact WHERE contactName like ?")) {
ps.setString(1, "%name1%");
try(ResultSet rs = ps.executeQuery()) {
while(rs.next()) {
//process your data
}
}
} catch(Exception e) {
//deal with it
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am working on javafx project and i want to create a loading sign on execution of my sql query but how could i know every part of execution of my query .
My code:
Thread df1=new Thread(){
public void run(){
for(int i=1;i<=1;i++){
try{
System.out.print("1%");
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/sql541774","sql541774","password");
String sql="Select * from messanger";
Statement stm=(Statement) con.createStatement();
ResultSet rs=stm.executeQuery(sql);
textmess.setText("");
while(rs.next()){
String sc=rs.getString(2);
String sc1=rs.getString(3);
String sc2=rs.getString(4);
}
rs.close();
stm.close();
con.close();
System.out.print("100%");
} catch(Exception e){
}
}
}
};
df1.start();
it shows me output
1% 100%
how i will able to get true output like:
1% 2% 3% .........98% 99% 100%
Please help me. Thank you.
First: remove the for loop, its useless.
Second: You cannot do that. There is no way to know in advance how long your statement will take. This depends on things like how many data you request, the network speed, other statements executed in the same moment or how much other load is on the machine.
What you could to is the following (in pseudo code): Request how many rows are there in total and get row by row. On each row, you can increase the loading bar. You can split the loading bar by the number of rows or you can calculate how many rows would be 1%.
int numberOfRows = SELECT COUNT(*) FROM MESSANGER;
for(i in numberOfRows)
row = SELECT * FROM MESSANGER WHERE ID = i
print i + " rows from " + numberOfRows + " rows loaded"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i want to get a list of databases stored in mysql and put in java table using command "show databases" via a resultset. but its not working.
DefaultTableModel model=(DefaultTableModel)dbTbl.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql//localhost/:3306","root","password");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("show databases;");
while(rs.next())
{
String db=rs.getString(1);
model.addRow(new Object[] {db});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"nahi chalda");
}
That's not the best way to get a list of Databases in JDBC. Here's the way it's done - using MetaData
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/:3306","root","password");
DatabaseMetaData meta = con.getMetaData();
ResultSet resultSet = meta.getCatalogs();
while (resultSet.next()) {
String db = resultSet.getString("TABLE_CAT");
model.addRow(new Object[] {db});
}
resultSet.close();
con.close();
See also: how to get list of Databases "Schema" names of MySql using java JDBC
I just forgot to add colon after "jdbc:mysql
Correct code is:
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/:3306","root","password");
It works ,......... !!!