Search

'MySQL'에 해당되는 글 6건

  1. 2014.10.20 mysql table datetime tip
  2. 2014.10.12 JSP, MYSQL, ECLIPSE 연동하기
  3. 2013.04.28 JAVA MYSQL ANDROID REST 활용
  4. 2013.04.28 JAVA MYSQL 활용 3
  5. 2013.04.27 JAVA MYSQL 활용 1
  6. 2013.04.27 JAVA MYSQL Test

mysql table datetime tip

Programming/JAVA,JSP 2014. 10. 20. 12:54 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


인스턴스 게시판을 만들던 도중


게시판에 글을 게시한 시간과 게시글을 수정한 시간을 표기하고자 하였다.



auto_increment 옵션을 주면 id가 자동으로 증가되면서 부여되듯이


시간도 insert, update 한 시점이 자동으로 업데이트되는 기능이 있을거라 생각하여 검색했다.



아래의 sql 문을 활용하면 insert, update 시 해당 컬럼이 자동으로 업데이트 된다.



작성일 DATETIME DEFAULT CURRENT_TIMESTAMP,
수정일 DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

'Programming > JAVA,JSP' 카테고리의 다른 글

Java Server/Client Code  (2) 2015.11.12
[Spring] 한글 인코딩 설정  (0) 2015.10.29
JSP, MYSQL, ECLIPSE 연동하기  (0) 2014.10.12
Java Chatting Server, Client  (0) 2014.07.12
JScrollPane , ScrollPane 차이점  (0) 2014.07.12

JSP, MYSQL, ECLIPSE 연동하기

Programming/JAVA,JSP 2014. 10. 12. 22:53 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

http://novon.tistory.com/entry/MySQL-%EC%9D%B4%ED%81%B4%EB%A6%BD%EC%8A%A4%EC%99%80-%EC%97%B0%EB%8F%99-%EC%84%A4%EC%A0%95-%EB%B0%8F-%EC%82%AC%EC%9A%A9%EB%B2%95



'Programming > JAVA,JSP' 카테고리의 다른 글

[Spring] 한글 인코딩 설정  (0) 2015.10.29
mysql table datetime tip  (0) 2014.10.20
Java Chatting Server, Client  (0) 2014.07.12
JScrollPane , ScrollPane 차이점  (0) 2014.07.12
Java ScrollPane 활용  (0) 2014.07.12

JAVA MYSQL ANDROID REST 활용

Programming/JAVA,JSP 2013. 4. 28. 17:15 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
REST 사용방법 http://192.168.0.3:8182/books http://192.168.0.3:8182/books/where/OReilly http://192.168.0.3:8182/books/ insert/"제목"/"저자"/"가격' REST 서버
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:3307/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");
			String publisher = rs.getString("publisher");
			String year = rs.getString("year");
			if (title != null) {
				result += id + " " + title + " " + publisher + " " + year
						+ "\n";
			}
		}
		return result;
	}

	public static String selectBooks(Statement stmt, String name)
			throws SQLException {
		ResultSet rs = stmt
				.executeQuery("SELECT * FROM books WHERE publisher = '" + name
						+ "'");
		String result = "";
		while (rs.next()) {
			int id = rs.getInt("book_id");
			String title = rs.getString("title");
			String publisher = rs.getString("publisher");
			String year = rs.getString("year");
			if (title != null) {
				result += id + " " + title + " " + publisher + " " + year
						+ "\n";
			}
		}
		return result;
	}

	@Override
	public Restlet createInboundRoot() {
		Router router = new Router();
		router.attach("http://localhost:8182/books", restlet1);
		router.attach("http://localhost:8182/books/where/{name}", restlet2);
		router.attach(
				"http://localhost:8182/books/insert/{title}/{publisher}/{year}/{price}",
				restlet3);
		router.attach("http://localhost:8182/datas/{year}/{month}/{day}",
				restlet4);
		router.attach("http://192.168.0.3:8182/books", restlet1);
		router.attach("http://192.168.0.3:8182/books/where/{name}", restlet2);
		router.attach(
				"http://192.168.0.3:8182/books/insert/{title}/{publisher}/{year}/{price}",
				restlet3);
		router.attach("http://192.168.0.3:8182/datas/{year}/{month}/{day}",
				restlet4);
		// (title, publisher, year, price)
		return router;
	}

	public Restlet restlet4 = new Restlet(getContext()) {
		@Override
		public void handle(Request request, Response response) {
			// Print the user name of the requested orders
			String year = (String) request.getAttributes().get("year");
			String month = (String) request.getAttributes().get("month");
			String day = (String) request.getAttributes().get("day");

			String message = "";
			try {
				message += selectDatas(stmt, year, month, day);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setEntity(message, MediaType.TEXT_PLAIN);
		}
	};

	public static String selectDatas(Statement stmt, String year, String month,
			String day) throws SQLException {
		ResultSet rs = stmt.executeQuery("SELECT * FROM datas WHERE year = "
				+ year + " && month =" + month + "&& day=" + day + ";");
		String result = "";
		while (rs.next()) {
			int id = rs.getInt("data_id");
			String contents = rs.getString("contents");
			if (contents != null) {
				result += id + " " + year + " " + month + " " + day + " "
						+ contents + "\n";
			}
		}
		return result;
	}

	public Restlet restlet3 = new Restlet(getContext()) {
		@Override
		public void handle(Request request, Response response) {
			// Print the user name of the requested orders
			String title = (String) request.getAttributes().get("title");
			String publisher = (String) request.getAttributes()
					.get("publisher");
			String year = (String) request.getAttributes().get("year");
			String price = (String) request.getAttributes().get("price");
			try {
				insertBooks(stmt, title, publisher, year, price);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String message = "";
			try {
				message += selectBooks(stmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setEntity(message, MediaType.TEXT_PLAIN);
		}

		private void insertBooks(Statement stmt, String title,
				String publisher, String year, String price)
				throws SQLException {
			// TODO Auto-generated method stub
			// INSERT INTO books (title, publisher, year, price)
			// VALUES('Operating System Concepts', 'Wiley', '2003', 30700);
			stmt.execute("INSERT INTO books (title, publisher, year, price)"
					+ "VALUES('" + title + "','" + publisher + "','" + year
					+ "'," + price + ");");
		}
	};

	public Restlet restlet2 = new Restlet(getContext()) {
		@Override
		public void handle(Request request, Response response) {
			// Print the user name of the requested orders
			String name = (String) request.getAttributes().get("name");
			String message = "";
			try {
				message += selectBooks(stmt, name);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setEntity(message, MediaType.TEXT_PLAIN);
		}
	};
	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 JNI  (0) 2013.05.01
Eclipse Tips  (0) 2013.04.30
JAVA MYSQL 활용 4 Android App  (0) 2013.04.28
JAVA MYSQL 활용 3  (0) 2013.04.28
MYSQL REST 실습 코드 2  (0) 2013.04.28

JAVA MYSQL 활용 3

Programming/JAVA,JSP 2013. 4. 28. 14:20 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
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");
			String publisher = rs.getString("publisher");
			String year = rs.getString("year");
			if( title != null)
			{
				result += id + " " + title +" " + publisher +" " + year + "\n";
			}
		}
		return result;
	}
	public static String selectBooks(Statement stmt, String name) throws SQLException {
		ResultSet rs = stmt.executeQuery("SELECT * FROM books WHERE publisher = '" + name + "'");
		String result = "";
		while (rs.next()) {
			int id = rs.getInt("book_id");
			String title = rs.getString("title");
			String publisher = rs.getString("publisher");
			String year = rs.getString("year");
			if( title != null)
			{
				result += id + " " + title +" " + publisher +" " + year + "\n";
			}
		}
		return result;
	}
	@Override
	public Restlet createInboundRoot() {
		Router router = new Router();
		router.attach("http://localhost:8182/books", restlet1);
		router.attach("http://localhost:8182/books/where/{name}", restlet2);
		router.attach("http://localhost:8182/books/insert/{title}/{publisher}/{year}/{price}", restlet3);
		//(title, publisher, year, price)
		return router;
	}

	public Restlet restlet3 = new Restlet(getContext()) {
		@Override
		public void handle(Request request, Response response) {
			// Print the user name of the requested orders
			String title = (String)request.getAttributes().get("title");
			String publisher = (String)request.getAttributes().get("publisher");
			String year = (String)request.getAttributes().get("year");
			String price = (String)request.getAttributes().get("price");
			try {
				insertBooks(stmt,title,publisher,year,price);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String message = "";
			try {
				message += selectBooks(stmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setEntity(message, MediaType.TEXT_PLAIN);
		}

		private void insertBooks(Statement stmt, String title,
				String publisher, String year, String price) throws SQLException {
			// TODO Auto-generated method stub
			//INSERT INTO books (title, publisher, year, price)
			//VALUES('Operating System Concepts', 'Wiley', '2003', 30700);
			stmt.execute("INSERT INTO books (title, publisher, year, price)"
					+"VALUES('"+title+"','"+publisher+"','"+year+"',"+price+");");
		}
	};

	public Restlet restlet2 = new Restlet(getContext()) {
		@Override
		public void handle(Request request, Response response) {
			// Print the user name of the requested orders
			String name = (String)request.getAttributes().get("name");
			String message = "";
			try {
				message += selectBooks(stmt, name);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.setEntity(message, MediaType.TEXT_PLAIN);
		}
	};
	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 ANDROID REST 활용  (0) 2013.04.28
JAVA MYSQL 활용 4 Android App  (0) 2013.04.28
MYSQL REST 실습 코드 2  (0) 2013.04.28
MYSQL & REST 실습 코드  (0) 2013.04.28
JAVA MYSQL 활용 1  (0) 2013.04.27

JAVA MYSQL 활용 1

Programming/JAVA,JSP 2013. 4. 27. 17:08 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. 데이터 삽입 INSERT


INSERT INTO books (title, publisher, year, price)

  VALUES('Operating System Concepts', 'Wiley', '2003', 30700);

INSERT INTO books (title, publisher, year, price)

  VALUES('Head First PHP and MYSQL', 'OReilly', '2009', 58000);

INSERT INTO books (title, publisher, year, price)

  VALUES('C Programming Language', 'Prentice-Hall', '1989', 35000);

INSERT INTO books (title, publisher, year, price)

  VALUES('Head First SQL', 'OReilly', '2007', 43700);



2. 데이터 확인 

(지난 코드 재실행)



'Programming > JAVA,JSP' 카테고리의 다른 글

MYSQL REST 실습 코드 2  (0) 2013.04.28
MYSQL & REST 실습 코드  (0) 2013.04.28
JAVA MYSQL Test  (0) 2013.04.27
JAVA 코드 그럴듯 하게 보이게 하기?!  (1) 2013.04.27
restlet download restlet-jse-2.1.2  (0) 2013.04.27

JAVA MYSQL Test

Programming/JAVA,JSP 2013. 4. 27. 17:05 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

JAVA MYSQL Test


1. MYSQL 설치


http://www.mysql.com/downloads/installer/


1.2 MYSQL JAVA 드라이버 설치


http://www.mysql.com/downloads/connector/j/


ZIP 파일 압축 해제 후




jar 파일 을 복사하여


내 컴퓨터의 


Java\jre7\lib\ext


폴더에 붙여넣기 합니다.





2. MYSQL Table 생성


DROP DATABASE book_db;

CREATE DATABASE book_db;

USE book_db;

CREATE TABLE books(

book_id INT NOT NULL auto_increment,

title VARCHAR(50),

publisher VARCHAR(30),

year VARCHAR(10),

price INT,

PRIMARY KEY(book_id)

);


3. TEST CODE





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

public class test {
	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 void main(String[] args) throws SQLException {
		Connection con = makeConnection();
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT * FROM books");
		while (rs.next()) {
			int id = rs.getInt("book_id");
			String title = rs.getString("title");
			System.out.println(id + " " + title);
		}
	}
}

3. 데이터베이스 연결 확인




'Programming > JAVA,JSP' 카테고리의 다른 글

MYSQL & REST 실습 코드  (0) 2013.04.28
JAVA MYSQL 활용 1  (0) 2013.04.27
JAVA 코드 그럴듯 하게 보이게 하기?!  (1) 2013.04.27
restlet download restlet-jse-2.1.2  (0) 2013.04.27
RESTLET Point  (0) 2013.04.21