标签:
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(); } } }
标签:
原文地址:http://www.cnblogs.com/zsboy/p/4295910.html