1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page import="java.sql.*" %> <html> <head> <title>My JSP starting page</title> </head>
<body> <span> <% try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //URL指向要访问的数据库名 String url = "jdbc:mysql://127.0.0.1:3306/enwords?serverTimezone=UTC"; //用户名 String user="***"; //密码 String password="***"; //建立连接 Connection con=DriverManager.getConnection(url,user,password); //创建状态 Statement state=con.createStatement(); //3.要执行的sql语句,需要读者自行修改 String sql="select * from enwords limit 100,120;"; //4.ResultSet类,用来存放获取的结果集!! ResultSet rs=state.executeQuery(sql);
String word=null; String translation=null; String aString=""; //读者请根据自己的数据库表修改打印内容。 while(rs.next()){ word=rs.getString("word"); translation=rs.getString("translation"); aString+=word+"\t"+translation+"<br>" +"----------------------------<br>"; } out.println(aString); state.close(); con.close();
} catch (Exception e) { e.printStackTrace(); } %> </span> </body> </html>
|