import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.restlet.Application;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
public class test extends Application{
public static Connection makeConnection() {
String url = "jdbc:mysql://localhost/book_db";
String id = "root";
String password = "green";
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("드라이버 적재 성공");
con = DriverManager.getConnection(url, id, password);
System.out.println("데이터베이스 연결 성공");
} catch (ClassNotFoundException e) {
System.out.println("드라이버를 찾을 수 없습니다.");
} catch (SQLException e) {
System.out.println("연결에 실패하였습니다.");
}
return con;
}
public static Statement stmt;
public static void main(String[] args) throws Exception {
Connection con = makeConnection();
stmt = con.createStatement();
Server server = new Server(Protocol.HTTP, 8182);
server.setNext(new test());
server.start();
}
public static String selectBooks(Statement stmt) throws SQLException {
ResultSet rs = stmt.executeQuery("SELECT * FROM books");
String result = "";
while (rs.next()) {
int id = rs.getInt("book_id");
String title = rs.getString("title");
if( title != null)
{
result += id + " " + title + "\n";
}
}
return result;
}
@Override
public Restlet createInboundRoot() {
Router router = new Router();
router.attach("http://localhost:8182/books", restlet1);
return router;
}
public Restlet restlet1 = new Restlet() {
public void handle(Request request, Response response) {
String message = "";
try {
message += selectBooks(stmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setEntity(message, MediaType.TEXT_PLAIN);
}
};
}
'Programming > JAVA,JSP' 카테고리의 다른 글
| JAVA MYSQL 활용 3 (0) | 2013.04.28 |
|---|---|
| MYSQL REST 실습 코드 2 (0) | 2013.04.28 |
| JAVA MYSQL 활용 1 (0) | 2013.04.27 |
| JAVA MYSQL Test (0) | 2013.04.27 |
| JAVA 코드 그럴듯 하게 보이게 하기?! (1) | 2013.04.27 |


syntaxhighlighter_3.0.83.zip
restlet-jse-2.1.2.vol1.egg