码迷,mamicode.com
首页 > 数据库 > 详细

JDBC

时间:2015-02-18 21:00:20      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

import static org.junit.Assert.*;

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

import org.junit.Test;


public class LianXi {

	@Test
	public void test() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Statement statement = null;
		String sql = null;
		
		String driverClass = "com.mysql.jdbc.Driver";
		String jdbcUrl = "jdbc:mysql:///test";
		String user = "root";
		String password = "root";
		
		try {
			Class.forName(driverClass);
			conn = DriverManager.getConnection(jdbcUrl,user,password);
			
			//插入数据
			sql = "INSERT INTO customer (name,birth,email) VALUES(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "abdc");
			ps.setDate(2, new Date(new java.util.Date().getTime()));
			ps.setString(3, "123@com");
			ps.executeUpdate();
			
			//查询结果集
			sql = "SELECT * FROM customer";
			statement = conn.createStatement();
			rs = statement.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String email = rs.getString("email");
				Date date = rs.getDate("birth");
				System.out.println(id+" "+name+" "+email+" "+date);
				
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

}

  

JDBC

标签:

原文地址:http://www.cnblogs.com/zsboy/p/4295910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!