Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC Error : unable to execute string in DB query
    text
    copied!<p>I want to pass <strong>strings</strong> as parameters to my <strong>JDBC</strong> program. The code works fine for <strong>INT</strong> type inputs but goes to catch block on string type inputs.</p> <p>I wanted to modify the program such that it can capture all types of inputs at random. Because, I need to run the following java prog. in bash script. So once class file is generated, i supposed to provide random inputs to the program and obtain the output...</p> <pre class="lang-java prettyprint-override"><code>import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Statement; public class JDBC_run { public static void main(String[] args) throws Exception { Connection conn = null; // int var = 123050044 ; String var = "sir"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&amp;password=root"); ResultSet rs; System.out.println("Connected?"); try{ Statement st = (com.mysql.jdbc.Statement) conn.createStatement(); rs = st.executeQuery("SELECT type FROM Table_new WHERE id = " + var); while ( rs.next() ) { String lastName = rs.getString("type"); System.out.println(lastName); } } catch(SQLException s){ System.out.println("wrong !!"); } } } </code></pre> <p>For <strong>int var = 123050044</strong>, the output is </p> <pre><code>Connected? student </code></pre> <p>For <strong>String var = "sir"</strong>, the output is </p> <pre><code>Connected? wrong !! </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload