I have been toiling with this issue all day. After reading the benefits between Statements(S) and PreparedStatements(PS) I decided to convert all my S s to PS s in Netbeans. I was astounded to see that there were no errors but...no output from the execution of my code either.
import java.sql.*;
public class ViewingMySQL {
public static void main(String[] args) {
//Declare Variables
Connection con;
ResultSet rs;
Statement stmt;
String sqlappname;
PreparedStatement findAppID_lookup= null;
String findAppID_lookup_stmt="select app.ID as APPID"
+" from IntergraphIN_AppTranslation"
+" inner join app on app.unit=IntergraphIN_AppTranslation.UnitName"
+" where IntergraphIN_AppTranslation.IntergraphUnitName=(?)";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:odbc:database","username","password");
sqlappname=" XXXY01 ";
findAppID_lookup= con.prepareStatement(findAppID_lookup_stmt);
findAppID_lookup.setString(1, sqlappname);
rs = findAppID_lookup.executeQuery();
if(rs.next()){
System.out.println(rs.getInt("APPID"));
}
rs.close();
findAppID_lookup.close();
}
catch(Exception e){
System.err.println(e);
}
}
}`
When the above code executes and builds...without output. run: BUILD SUCCESSFUL (total time: 1 second)
我原来是:
import java.sql.*;
public class ViewingMySQL {
public static void main(String[] args) {
//Declare Vars
Connection con;
ResultSet rs;
Statement stmt;
String sqlappname;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:odbc:Database","username","password");
sqlappname=" XXXY01 ";
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT ID FROM app where AppName="+sqlappname);
if(!rs.isBeforeFirst()){
rs.close();
rs = stmt.executeQuery("select app.ID from IntergraphIN_AppTranslation"
+" inner join app"
+" on app.unit=IntergraphIN_AppTranslation.UnitName"
+" where IntergraphIN_AppTranslation.IntergraphUnitName="+sqlappname);
}
if(rs.next()){
System.out.println(rs.getInt(1));
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
System.err.println(e);
}
}
}`
This code outputs: run: 2020603 BUILD SUCCESSFUL (total time: 1 second)
见上文注1。
Can someone please help me with understanding what I m doing...is it the formatting of the variable that I m passing into the PS? Thanks, SCorliss