简体   繁体   中英

Spring Boot mysql Query without DAO

i'm using Spring Boot for developing a browser based application. There i want to process the results of many different SELECT query's.

Is there a way to query a mysql database without the need of using DAO?

I just want to send a query (like "SELECT * FROM table1;) to the database and receiving something like an array of strings with the rows of my query result. If i can access each column per row easily would be great too.

On the web i found this, what would be what i'm looking for: http://alvinalexander.com/java/edu/pj/jdbc/jdbc0003 But it seems not to work on spring.

Would be great to get some advice.

I actually use code like this:

package app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    //DB Connection
    @Autowired
    JdbcTemplate sql;

    @RequestMapping("/test_db_interaction")
    public String DB_Interacton(){

        //example query execution
        sql.execute("CREATE TABLE IF NOT EXISTS table1...)");

        return "success_page";
    }

}

It would be good to use the JdbcTemplate again for SELECT statements, so that i do not have to configure my DB connection again.

I found my solution. This is working great for me:

SqlRowSet rowSet = sql.queryForRowSet("SELECT * from table1");

while(rowSet.next()){
String col1 = rowSet.getString("col1");
String col2 = rowSet.getString("col2");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM