连接数据库
下载 Java 专用的连接 MySQL 的驱动包 JDBC (jar 包),复制到 lib 里面
例子
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
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.mysql.jdbc.Driver" %> <%@ page import="java.sql.*" %> <% String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String userPasswd = "root"; String dbName = "test"; String tableName = "users"; String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); String sql="SELECT * FROM "+tableName; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { out.print(rs.getString(1)+"<br>"); } rs.close(); stmt.close(); conn.close(); %>
|
查询操作
statment.executeQuery();
用于产生单个结果集的语句,例如 SELECT 语句。最常用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.mysql.jdbc.Driver, java.sql.*" %> <% String url = "jdbc:mysql://localhost/test?user=root&password=root"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); String sql="SELECT * FROM users"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()) { out.print(rs.getString(1)+"<br>"); }
rs.close(); stmt.close(); conn.close(); %>
|
数据操作
statment.executeUpdate();
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言,例如 CREATE TABLE 和 DROP TABLE)语句。
- INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列,executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。
- 对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.mysql.jdbc.Driver, java.sql.*" %> <% String url="jdbc:mysql://localhost/test?user=root&password=root"; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn=DriverManager.getConnection(url); Statement stmt = conn.createStatement();
String sql="INSERT into users (username, id) values ('hhhhhhhh', '111')"; stmt.executeUpdate(sql);
stmt.close(); conn.close(); %>
|
高级操作
statment.execute();
用于执行返回多个结果集、多个更新计数或二者组合的语句。
execute方法应该仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。当执行某个已存储过程或动态执行未知 SQL 字符串(即应用程序程序员在编译时未知)时,有可能出现多个结果的情况,尽管这种情况很少见。
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
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.mysql.jdbc.Driver, java.sql.*" %> <% stmt.execute(queryStringWithUnknownResults);
while (true) { int rowCount = stmt.getUpdateCount(); if (rowCount > 0) { System.out.println("Rows changed = " + count); stmt.getMoreResults(); continue; } if (rowCount == 0) { System.out.println(" No rows changed or statement was DDLcommand"); stmt.getMoreResults(); continue; } ResultSet rs = stmt.getResultSet; if (rs != null) { while (rs.next()) { stmt.getMoreResults(); continue; } break; } } %>
|