日期:2014-05-16 浏览次数:20503 次
Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/books?useUnicode=true&characterEncoding=GBK","root","weishenme2"); Statement stmt=con.createStatement(); ResultSet rst=stmt.executeQuery("select * from book"); while(rst.next()) { System.out.println(rst.getString("bookId")); System.out.println(rst.getString("bookName")); System.out.println(rst.getString("publisher")); System.out.println("price"); }
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/young"/> <property name="username" value="root"/> <property name="password" value="weishenme2"/> </bean>
final ArrayList<User> users=new ArrayList<User>(); Resource resource = new ClassPathResource("dao/jdbc/spring.xml"); BeanFactory factory = new XmlBeanFactory(resource); DriverManagerDataSource ds=(DriverManagerDataSource) factory.getBean("ds"); JdbcTemplate jt=new JdbcTemplate(ds); jt.query("SELECT * FROM user", new RowCallbackHandler(){ public void processRow(ResultSet rs) throws SQLException { User user=new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); users.add(user); } });