Java JDBC MySQL Connection query(String sql)

Here you can find the source of query(String sql)

Description

query

License

Apache License

Declaration

public static ResultSet query(String sql) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    private static Connection mConn = null;
    private static Statement mStmt = null;

    public static ResultSet query(String sql) {
        try {// w w  w .j a v  a 2  s  . c om
            if (mStmt == null || mStmt.isClosed()) {
                openConnection();
            }
            return mStmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void openConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            mConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/source_products", "root", "123");
            mStmt = mConn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. getNewDatabaseConnection(String dbUser, String dbPassword)
  2. getRssminerDB()
  3. getSearchReaderConnection()
  4. openConnection()
  5. putData(int tID, String toolID)
  6. queryAll(String sql, List params)
  7. readMapListBySQL(String driverClass, String host, String port, String database, String user, String password, String sql)
  8. removeById(int Id)
  9. searchForItemId(String item)

  10. HOME | Copyright © www.java2s.com 2016