SQL Exception: java.sql.SQLException: No data found - java

i get an error wherein it says SQL Exception: java.sql.SQLException: No data found, i cant seems to find the problem here. please help me, sorry for asking.
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:ict11";
Connection con = DriverManager.getConnection(url);
Statement statement = con.createStatement();
statement.executeUpdate( "DELETE from Employee where EmployeeID ="+txtId.getText()+"" );
statement.close();
con.close();
JOptionPane.showMessageDialog(rootPane, "Successfully Deleted");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

I can think about 2 issues
This could be because of unnecessary white spaces that getText() doesn't eliminate. Try txtId.getText().trim()
URL might be wrong.
Apart from that, do the following to improve the code.
Print complete stack trace
Use PreparedStatement instead of statement.

statement.executeUpdate( "DELETE from Employee where EmployeeID ="+txtId.getText()+"" );
try using this
statement.executeUpdate( "DELETE from Employee where EmployeeID ='"+txtId.getText()+"'" );
note the addition of single inverted comma at the start and end of txtId.getText()

Related

adding Database Values using Java gui

So I'm just really new with the whole database getting connected to Java and for my project i decided to integrate some gui since it's seems to make things easier and user friendly. For some reason, I can't add values because I'm getting asked where the VALUES keyword is even though it's there. Can someone help? really lost here :(
try{
//get connection
DBconnection dbconn = new DBconnection();
dbconn.login("homeuser", "12345");
dbconn.connect();
con =dbconn.getConnection();
//insert values
Statement statement = con.createStatement();
String sql =("INSERT INTO StudentInfo"+
"VALUES('first_name,'last_name,'StudID)");
statement.executeUpdate(sql);
JOptionPane.showConfirmDialog(rootPane, "Record Added.");
con.commit();
first.setText("");
last.setText("");
idText.setText("");
}catch(SQLException sqlex){
System.out.println(sqlex.getErrorCode()+" "+sqlex.getMessage());
}
You are missing a space. added after StudentInfo. check now.
//insert values
Statement statement = con.createStatement();
String sql =("INSERT INTO StudentInfo "+
"VALUES('first_name’,'last_name’,'StudID’)");
statement.executeUpdate(sql);

difficult time inserting data with textfield( jdbc swing)

My insert into is giving me issues.
Im pretty sure the Jtextfield is the problem.
If anyone can give me a nudge in the right direction I would greatly appreciate it.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#//localhost:1521/xe";
Connection conn = DriverManager.getConnection(url, "system", "oracle");
conn.setAutoCommit(false);
String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";
Statement stm = conn.createStatement();
stm.execute(query1);
JOptionPane.showMessageDialog(null, "Record Added Succesfully.", "Record Added",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
System.out.println(e);
}
}
nameTextField.getSelectedText()
I'm guessing that should be
nameTextField.getText()
Also, use a PreparedStatement for the SQL. You will less likely to make SQL syntax errors. Check out: geting data from multiple table when selecting a row then clicking a button for a basic example.
Im pretty sure the Jtextfield is the problem.
Well, learn how to do some basic debugging instead of guessing.
String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";
The above code can be written like:
String name = textField.getSelectedText();
// now you can verify the value of the name
System.out.println(name);
String query1 = "INSERT INTO WIN(NAME) VALUES('"+ name +"')";
// verify SQL syntax
System.out.println( query1);
Of course my first suggestion to use the PreparedStatement is easier, but learn how to display the value of variables when you do debugging.

how to validate a username value with mysql and java to avoid duplicate user name

good day, i have a problem with this method, for some reason it gives me an error. "Java.sql.SqlException: statement is not executing". But it checks if the value is duplicate and prompts the error as the method below show. This has stopped my registration from completing. thanks for your help
public static void UserExists(String y){
try{
query = " select * from MailRegister where Username=?";
pst = connect.prepareStatement(query); //passes my query to java predefined prepared statement
pst.setString(1, Username.getText()); //passes the value of the username to the prepared statement
rs = pst.executeQuery(); //this would execute the query passed in the prepared statement
if(rs.next()){
JOptionPane.showMessageDialog(null, " Sorry This Username is Taken");
}
pst.close();
rs.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);//shows error dialog
}
}
You probably get this error because your objects are not closed properly all the time. Your connect is breaking. A good practice is to open and close the connection when required to prevent leaks.
In the code above, pst.close(); and rs.close(); should be in a finally.
Or even cleaner, the prepared statement should go inside a :
try (pst = connect.prepareStatement(query)) {...}
That way you don't have to close it yourself, the JVM will handle it for you.

Insert SQL in Java syntax

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pizzabaseaccount", "root", "");
Statement st = con.createStatement();
String sql ="insert into pizzaorder (PizzaType,PizzaChosen,ToppingsDetails,CrustType,PizzaSize,PizzaQuantities,PizzaTotalPrice)"
+"values ('"+pizzatype+"','"+pizzachosen+"','"+toppingsdetails+"','"+crusttype+"','"+pizzasize+"','"+pizzaquantities+"','"+totalprice+"')";
st.executeQuery(sql);
JOptionPane.showMessageDialog(null, "Order Complete");
}catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error");
}
I get "Error". It wont insert the data on the pizzabaseaccount database. pizzaorder is the table name btw. I don't know whats wrong with the code. Im a beginner on Java. Btw, the pizzatype , pizzachosen, toppingdetails, crusttype, pizzasize, pizzaquantities, and totalprice are String variables.
Check that pizzatype pizzachosen and all the others do not contains the "'" carachter; if so, you should escape it.
Anyway, this is not a good way to create an SQL query.
Use bind parameters --> read the API reference

I can't connect to my database using java

Connection conn = null;
Statement stmt=null;
ResultSet rset=null;
String jdbc_url="jdbc:oracle:thin:hr/hr#bassam-desktop:1521:XE";
String query="";
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn=DriverManager.getConnection(jdbc_url);
stmt=(Statement) conn.createStatement();
query="select first_name" +"from employees"+"where employeed_id=100";
rset=stmt.executeQuery(query);
System.out.println(rset.getString(1));
}
catch(SQLException | NumberFormatException e)
{
System.out.println("result error");
}
finally{
try{
rset.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println("Error in closing");
}
}
I'm getting both errors in the results window, what is the problem? hr password is correct, my host name is correct, isn't it the one you get from "Computer Name" after you right click on "My Computer" in windows xp ?
Edit: After using e.getMessage(), i got this..
result error, ResultSet.next was not called
The error message of the exception, that you have put in the comments, tells you what's wrong:
result error, ResultSet.next was not called
You forgot to call ResultSet.next() before accessing the first row of the result set:
if (rset.next()) {
System.out.println(rset.getString(1));
}
Because you're using Netbeans IDE, you can try connecting with their built in tool. That should help you get the jdbc_url right (which is almost certainly the problem). Also, don't forget to download and include in your classpath the ojdbc6.jar (which would definitely be the problem if you're not including it).
The e variable represent the exception. try to print the e.getMessage() or print the stack trace with e.printStackTrace() to understand better the problem.
ResultSet rset = stmt.executeQuery();
while(rset.next()){
//Code that works with results
}

Categories

Resources