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"
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 5 years ago.
Improve this question
stmt.executeUpdate("update fees set term_1 = "+hm.get("term_1").toString()+" term_2 ="+hm.get("term_1").toString()+"total = "+hm.get("total").toString()+"id = "+std_id);
Why it is not working when it is connected to JDBC?
Your update statement is invalid, you are missing comma(,)
Correct SQL Update statement should be
update fees set term_1 = 'something', term_2='something', total='something' where id = something;
So your final Java statement will be like:
stmt.executeUpdate( " Update fees set term_1 = '"+hm.get("term_1").toString()+"',"
+ " term_2 ='"+hm.get("term_1").toString()+"',"
+ " total = "+hm.get("total").toString()+"'"
+ " where id ="+std_id);
Note : Assuming all columns apart from id are of String type (i.e. term_1,term_2,total)
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 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.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to learn how to execute basic SQL commands from a Java application. I have been searching for hours and all i can see are stuff to connect to a database. Can anyone provide a sample code to save instances of a simple class containing two fields, a Name(String) and Id(Int) in java.
The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database.
The following simple code fragment gives a simple example of these three steps:
public void connectToAndQueryDatabase(String username, String password) {
Connection con = DriverManager.getConnection(
"jdbc:myDriver:myDatabase",
username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
int x = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
}
}
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 need your help in creating a java bean to retrieve the first 4 columns in a table called "m_connection". I want to retrieve the first 4 columns and store them in variables.
They are all string.
this code sample may help you:
String sql="select one,two,three,four from table where id=1";
Connection con = DriverManager.getConnection("");//get connection here
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
String[] result = new String[4];
while(rs.next()){
result[0] = rs.getString(1);
result[1] = rs.getString(2);
result[2] = rs.getString(3);
result[3] = rs.getString(4);
}
First of all you need to establish jdbc connection then think for the data retrival
for jdbc connection you need to follow
Load the JDBC driver.
Define the connection URL.
Establish the connection.
Create a statement object.
Execute a query or update.
Process the results.
Close the connection.
refer details