So stellen Sie eine Verbindung von STS (Spring Tool Suite) zur Datenbank (Oracle) herJava

Java-Forum
Anonymous
 So stellen Sie eine Verbindung von STS (Spring Tool Suite) zur Datenbank (Oracle) her

Post by Anonymous »

Ich bin neu im Frühling. Ich versuche, meinen ersten Webdienst zu erstellen, und ich möchte eine Verbindung zu einer Datenbank herstellen, Daten abrufen und als JSON zurückgeben. Wildfly 8.2.0. < /P>

Ich möchte eine Verbindung zur Oracle 11g -Datenbank herstellen und SQL -Abfragen ausführen. Kannst du mir helfen? < /P>

Mein Code ist unten veröffentlicht.package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

}
< /code>

greeting.java:

package hello;

public class Greeting {

private final long id;
private final String content;

public Greeting(long id, String content) {
this.id = id;
this.content = content;
}

public long getId() {
return id;
}

public String getContent() {
return content;
}
}
< /code>

GreetingController.java:

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping(value="/greeting",method=RequestMethod.GET)
public List greeting(@RequestParam(value="name", defaultValue="World") String name,
@RequestParam(value="content", defaultValue="World") String content,HttpServletResponse response) {
List list_greet = new ArrayList();
list_greet.add(new Greeting(counter.incrementAndGet(),
String.format(template, name)));
list_greet.add(new Greeting(counter.incrementAndGet(),
String.format(template, content)));
list_greet.add(new Greeting(counter.incrementAndGet(),
String.format(template, name)));
list_greet.add(new Greeting(counter.incrementAndGet(),
String.format(template, name)));
//to have webservice work,beacause cors cut it out.
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

return list_greet;
}
}
< /code>

pom.xml:



4.0.0

org.springframework
gs-rest-service
0.1.0
war


org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE




org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-tomcat
provided




1.8





org.springframework.boot
spring-boot-maven-plugin






spring-releases
https://repo.spring.io/libs-release




spring-releases
https://repo.spring.io/libs-release



< /code>

Danke im Voraus! < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post