Parametrized Query Using JDBC

Jonathan Solorzano

Is it possible to create a standard method to execute parametrized queries from Java to SQL Server using JDBC?

Here's how i create a pool of connections to the db:

    static ConnectionPool pool;
    public static void crearPool(){
        try {
            Class c = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Driver driver = (Driver) c.newInstance();
            DriverManager.registerDriver(driver);
            String url = "jdbc:sqlserver://localhost:1433;database=STAZIONE;";
            pool = new ConnectionPool("local",5, 20, 40, 180, url, "b_lightyear", "BeyondInfinity");
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException ex) {/*Error Message*/}
    }

And here's how i think a standard method for every DML Query would be:

    public static int QueryDML(String consulta,Object []data){
        int result = 0;
        PreparedStatement prpdStm = null;
        try{
            Connection cnx = pool.getConnection(clsConexion.espera);
            if(conexion!=null){
                prpdStm = conexion.prepareStatement(consulta,,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                for(int position =1; position<data.length;position++){
                    prpdStm.setObject(position, data[position-1]);
                }result = prpdStm.executeUpdate();
            } else{/*Message*/}
        } catch (SQLException ex) {/*Error Message*/}
        return result;
    }

What i want to know is if prpdStm.setObject(position, data[position-1]); will function for any type of data ->(String, Integer, Double, etc...)?

Elliott Frisch

What i want to know is if prpdStm.setObject(position, data[position-1]); will function for any type of data ->(String, Integer, Double, etc...)?

Short answer: Yes.

Slightly longer answer per the PreparedStatement.setObject() JavaDoc -

The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parametrized Query Using JDBC

From Dev

Conversion to recommended parametrized query method

From Dev

Parametrized query unknown total params

From Dev

How to print a parametrized SQL query?

From Dev

MySQL parametrized query with like operator

From Dev

Using parametrized parameters WITH sqlcommandbuilder [no dataadapter]

From Dev

Mocking Parametrized Constructor using Gmock

From Dev

Parametrized like query in neo4j

From Dev

Parametrized Linq to SQL query causes performance issues

From Dev

C# SQLCommand - Parametrized query is cut short

From Dev

Postgre SQL parametrized query for C#

From Dev

Unable to query database correctly using JDBC

From Dev

Parametrized job using build pipeline plugin on Jenkins

From Dev

dynamic parametrized queries using Spring data mongodb

From Dev

Using RoboSpice/Retrofit with Parametrized types for JSON Response

From Dev

Multiple Selenium parametrized webtests using Java

From Dev

How can I build a parametrized query without concatenation?

From Dev

Inserting into MySQL with python via parametrized query leads to error

From Dev

How to use dynamic table name in SELECT query using JDBC

From Dev

Wrong number of args (4) passed to: jdbc/query using Korma

From Dev

JDCB error using xPages Extension Library JDBC Query

From Dev

Filemaker SQL query with question mark in column name using JDBC

From Dev

How to use dynamic table name in SELECT query using JDBC

From Dev

Ambiguous column using JDBC but query works fine in database

From Dev

Null Pointer Exception while executeUpdate for a query using JDBC

From Dev

Wrong number of args (4) passed to: jdbc/query using Korma

From Dev

Converting Postgres Query Plan into xml and store in file using Eclipse JDBC

From Dev

"No default Typeable for parametrized type" using Shapeless 2.1.0-RC2

From Dev

WebDriver. Not able to run tests after using parametrized class

Related Related

  1. 1

    Parametrized Query Using JDBC

  2. 2

    Conversion to recommended parametrized query method

  3. 3

    Parametrized query unknown total params

  4. 4

    How to print a parametrized SQL query?

  5. 5

    MySQL parametrized query with like operator

  6. 6

    Using parametrized parameters WITH sqlcommandbuilder [no dataadapter]

  7. 7

    Mocking Parametrized Constructor using Gmock

  8. 8

    Parametrized like query in neo4j

  9. 9

    Parametrized Linq to SQL query causes performance issues

  10. 10

    C# SQLCommand - Parametrized query is cut short

  11. 11

    Postgre SQL parametrized query for C#

  12. 12

    Unable to query database correctly using JDBC

  13. 13

    Parametrized job using build pipeline plugin on Jenkins

  14. 14

    dynamic parametrized queries using Spring data mongodb

  15. 15

    Using RoboSpice/Retrofit with Parametrized types for JSON Response

  16. 16

    Multiple Selenium parametrized webtests using Java

  17. 17

    How can I build a parametrized query without concatenation?

  18. 18

    Inserting into MySQL with python via parametrized query leads to error

  19. 19

    How to use dynamic table name in SELECT query using JDBC

  20. 20

    Wrong number of args (4) passed to: jdbc/query using Korma

  21. 21

    JDCB error using xPages Extension Library JDBC Query

  22. 22

    Filemaker SQL query with question mark in column name using JDBC

  23. 23

    How to use dynamic table name in SELECT query using JDBC

  24. 24

    Ambiguous column using JDBC but query works fine in database

  25. 25

    Null Pointer Exception while executeUpdate for a query using JDBC

  26. 26

    Wrong number of args (4) passed to: jdbc/query using Korma

  27. 27

    Converting Postgres Query Plan into xml and store in file using Eclipse JDBC

  28. 28

    "No default Typeable for parametrized type" using Shapeless 2.1.0-RC2

  29. 29

    WebDriver. Not able to run tests after using parametrized class

HotTag

Archive