2017-04-01 96 views
1

我在我的项目中创建了一个数据库连接类,以便在项目中的所有类中创建和共享数据库实例。使用此代码在java mysql数据库连接类中实现一个使用preparedStatements的方法

void createConnection(){ 
    try{ 
     connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", ""); 
     System.out.println("Connected"); 
    }catch(Exception e) { 
     System.out.println("Connection Failed"); 
     System.out.println(e); 
    } 

} 

然后获得

数据库连接我创建使用准备的语句如下

public boolean execAction(String query){ 
    try { 
     pstatement = (PreparedStatement) connection.prepareStatement(query); 
     pstatement.execute(query); 
     System.out.println("Values inserted"); 
     return true; 
    } catch (SQLException ex) { 
     Logger.getLogger(databaseHandler.class.getName()).log(Level.SEVERE, null, ex); 
     System.out.println("Values insertion failed"); 
     return false; 
    } finally { 

    } 

我想找到一种方法来传递的PreparedStatement变量值插入值到数据库的方法到这个方法。

+0

你的'execAction()'泛型方法接受任何值或目标为一个表? – developer

+0

它是一种可以接受任何值的通用方法 –

回答

0

如果您正在寻找execAction(String query)来处理任何数据库表,那么你就需要编写大量复杂的逻辑,以检查其数据类型(即,一滴,整型,字符串,等等。),它是不是一个好想再次重新发明一些东西。它可以很好的和容易处理,如果你使用任何的ORM(对象关系映射)如Hibernate工具等

的ORM的概念是,你需要映射每个模型(实体)类的表在数据库和框架将处理所有样板代码(即,添加preparedstatement,setString(),setInt()等)。

如果我通过整体项目共享 数据库类实例,是否有办法在另一个类中获取连接对象?

它不是共享一个连接对象在整个应用程序,而return不同的连接对象,如下图所示,同时,还要确保你需要关闭通过调用连接的close()一个很好的做法。

还有一点是,您可能需要设置连接池,否则最终会出现错误。

public class DBUtil { 

    public static Connection createConnection(){ 
    try{ 
     Connection connection = (Connection) DriverManager. 
       getConnection("jdbc:mysql://localhost:3306/testdb", 
        "root", ""); 
     System.out.println("Connected"); 
     return connection; 
    }catch(Exception e) { 
     System.out.println("Connection Failed"); 
     System.out.println(e); 
    } 
} 

    public static void closeConnection(Connection conn) { 
     conn.close(); 
    } 
} 
+0

如果通过整体项目共享数据库类实例,是否有方法可以在另一个类中获取连接对象? –

0

我想找到一种方法,通过PreparedStatement的变量值此方法。

通常,您可以将变量作为参数传递给另一个方法,或者如果在类级别声明变量,则可以访问变量。

我不知道查询及其提供参数的确切细节。请参阅解释使用Prepared Statement的示例。

Example:见链接

private static void insertRecordIntoTable() throws SQLException { 

    Connection dbConnection = null; 
    PreparedStatement preparedStatement = null; 

    String insertTableSQL = "INSERT INTO DBUSER" 
      + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" 
      + "(?,?,?,?)"; 

    try { 
     dbConnection = getDBConnection(); 
     preparedStatement = dbConnection.prepareStatement(insertTableSQL); 

     preparedStatement.setInt(1, 11); 
     preparedStatement.setString(2, "mkyong"); 
     preparedStatement.setString(3, "system"); 
     preparedStatement.setTimestamp(4, getCurrentTimeStamp()); 

     // execute insert SQL stetement 
     preparedStatement.executeUpdate(); 

     System.out.println("Record is inserted into DBUSER table!"); 

    } catch (SQLException e) { 

     System.out.println(e.getMessage()); 

    } finally { 

     if (preparedStatement != null) { 
      preparedStatement.close(); 
     } 

     if (dbConnection != null) { 
      dbConnection.close(); 
     } 

    } 

} 

有没有办法获取连接对象另一个类,如果我通过整个项目共享一个数据库类实例的详细信息?

如果您想要在不同的类或项目级别之间共享连接,可以使用单例模式创建连接对象,这样可以灵活地为所有类创建一个连接。

0

我的建议是: 首先想想你需要什么方法去做。它只需要采取单一的值,还是很多?参数是相同类型的吗?

一旦你有了这些,决定什么数据结构最能代表这一点。由于PreparedStatement通过索引来引用参数,如果您想要很多值,您可能需要某种列表。如果每个条目也需要一个类型,那么您可能需要创建一个数据类来将该值和值保存在一个地方。

因为这看起来像一个编程作业,我不会写代码的你,但会给我这样的事情作为一个起点:

public class DatabaseHandler { 
    public boolean execAction(String query, List<PreparedStatementArgument> arguments){ 
     // code here 
    } 
} 

class PreparedStatementArgument { 
    private final PreparedStatementArgumentType type; 
    private final Object value; 

    // Constructor, getters 
} 

enum PreparedStatementArgumentType { 
    INT, STRING; //etc 
} 

您也可以考虑使用多态的PreparedStatementArguments,这取决于你准备得到多少复杂。

如果这个不是一个编程任务,请注意,这样的方法的问题是它不能验证它给出的与SQL匹配的参数 - 这使得难以发现如果有一个不匹配,并且很可能会使您的应用程序在长期运行中更容易出错。