try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
System.out.println(“” + e);
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
stmt = con.createStatement();
rs = stmt.executeQuery(“SELECT * FROM bookdata”);
while (rs.next()) {
String isbn = rs.getString(“isbn”);
String name = rs.getString(“name”);
String authors = rs.getString(5);
float price = rs.getInt(“price”);
int pages = rs.getInt(“pages”);
System.out.print(“书号:” + isbn);
System.out.print(" 书名:" + name);
System.out.print(" 作者:" + authors);
System.out.print(" 定价:" + price);
System.out.println(" 页数:" + pages);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子2:条件查询
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
stmt = con.createStatement();
rs = stmt.executeQuery(“SELECT name,price FROM bookdata WHERE price >= 30”);
while (rs.next()) {
String name = rs.getString(1);
double price = rs.getInt(“price”);
System.out.print(" 书名:" + name);
System.out.println(" 定价:" + price);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子3:可滚动结果集
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
Statement stat;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
System.out.println(“” + e);
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stat.executeQuery(“SELECT name,pages FROM bookdata”);
rs.last();
int number = rs.getRow();
System.out.println(“该表共有” + number + “条记录”);
rs.afterLast();
while (rs.previous()) {
String name = rs.getString(“name”);
int pages = rs.getInt(“pages”);
System.out.print(" 书名:" + name);
System.out.println(" 页数:" + pages);
}
System.out.println(“单独输出第5条记录:”);
rs.absolute(5);
String name = rs.getString(“name”);
int pages = rs.getInt(“pages”);
System.out.print(" 书名:" + name);
System.out.println(" 页数:" + pages);
rs.close();
stat.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子4:排序结果集
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
sql = con.createStatement();
String condition = “SELECT name, pages FROM chengjibiao ORDER BY pages”;
rs = sql.executeQuery(condition);
while (rs.next()) {
String name = rs.getString(1);
int pages = rs.getInt(2);
System.out.print(" 姓名:" + name);
System.out.println(" 页数:" + pages);
}
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子5:模糊查询
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”,
“123”);
sql = con.createStatement();
rs = sql.executeQuery("SELECT name,price FROM bookdata WHERE name LIKE ‘%java%’ ");
while (rs.next()) {
String name = rs.getString(1);
double price = rs.getInt(2);
System.out.print(" 书名:" + name);
System.out.println(" 定价:" + price);
}
rs.close();
sql.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子6:计算抽样图书的平均页数。
import java.sql.*;
import java.util.LinkedList;
public class DatabaseTest {
public static void main(String args[]) {
LinkedList list = new LinkedList();
Connection con;
Statement sql;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(“SELECT name,pages FROM bookdata”);
rs.last();
int lownumber = rs.getRow();
int number = lownumber;
for (int i = 1; i <= number; i++) {
list.add(new Integer(i));
}
double sum = 0;
int k = 4;
int 抽取数目 = k;
while (k > 0) {
int i = (int) (Math.random() * list.size());
int index = ((Integer) list.get(i)).intValue();
rs.absolute(index);
System.out.print(“书名:” + rs.getString(1));
System.out.println(“页数:” + rs.getString(2));
int math = rs.getInt(“pages”);
sum = sum + math;
k–;
list.remove(i);
}
System.out.println(“抽样的图书平均页数:” + sum / 抽取数目);
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子7:图书信息的增加、修改、删除、查询。
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”,
“123”);
stmt = con.createStatement();
int pages = 299;
String isbn = “9877811234169”, name, authors, publisher;
String updateStr = “UPDATE bookdata SET pages =” + pages
- " WHERE isbn = " + “'” + isbn + “'”;
pages = 400;
float price = 45.8f;
isbn = “9877811239999”;
name = “Java高级数据库编程”;
authors = “张三,李四,王五”;
publisher = “蓝天出版社”;
String value = “(” + “'” + isbn + “', '” + name + “', '” + authors
- “', '” + publisher + "', " + pages + “,” + price + “)”;
String insertStr = "INSERT INTO bookdata(isbn, name, authors, publisher, pages, price) VALUES " + value;
String delStr = "DELETE FROM bookdata WHERE isbn = ‘9877811239999’ ";
stmt.executeUpdate(updateStr);
stmt.executeUpdate(insertStr);
stmt.executeUpdate(delStr);
rs = stmt.executeQuery(“SELECT * FROM bookdata”);
while (rs.next()) {
isbn = rs.getString(“isbn”);
name = rs.getString(“name”);
authors = rs.getString(5);
publisher = rs.getString(“publisher”);
pages = rs.getInt(“pages”);
price = rs.getFloat(“price”);
System.out.print(“书号:” + isbn);
System.out.print(" 书名:" + name);
System.out.print(" 作者:" + authors);
System.out.print(" 作者:" + publisher);
System.out.print(" 页数:" + pages);
System.out.println(" 定价:" + price);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子8:使用预处理语句prepareStatement查询数据库
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
PreparedStatement pstmt;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
System.out.println(“” + e);
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
pstmt = con.prepareStatement(“SELECT isbn, price FROM bookdata”); // 预处理语句
rs = pstmt.executeQuery();
while (rs.next()) {
String isbn = rs.getString(1);
float price = rs.getFloat(2);
System.out.print(“书号:” + isbn);
System.out.println(" 定价:" + price);
}
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子9:使用预处理语句prepareStatement增加、修改、删除、
import java.sql.*;
public class DatabaseTest {
public static void main(String args[]) {
Connection con;
PreparedStatement pstmt1, pstmt2, pstmt3;
ResultSet rs;
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
} catch (ClassNotFoundException e) {
System.out.println(“” + e);
}
try {
con = DriverManager.getConnection(“jdbc:odbc:library”, “book”, “123”);
pstmt1 = con.prepareStatement("UPDATE bookdata SET price = ? WHERE id= ? ");
pstmt1.setFloat(1, 55.5f);
pstmt1.setInt(2, 1);
pstmt1.executeUpdate();
pstmt1.setInt(1, 2);
pstmt1.setFloat(2, 66.6f);
pstmt1.executeUpdate();
pstmt2 = con.prepareStatement(“INSERT INTO bookdata(isbn, name, pages, price) VALUES (?,?,?,?)”);
pstmt2.setString(1, “20038881111”);
pstmt2.setString(2, “Oracle数据库使用指南”);
pstmt2.setInt(3, 300);
pstmt2.setFloat(4, 77.7f);
pstmt2.executeUpdate();
pstmt3 = con.prepareStatement("SELECT name, pages, price FROM bookdata WHERE pages <= ? AND price >= ? ");
pstmt3.setInt(1, 400);
pstmt3.setFloat(2, 50f);
System.out.println(“3…”);
rs = pstmt3.executeQuery();
while (rs.next()) {
System.out.print(“书名:” + rs.getString(1));
System.out.print(" 页数:" + rs.getInt(2));
System.out.println(" 定价:" + rs.getFloat(3));
}
con.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
例子10:综合例子
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client
{
public static void main(String args[])
{ new QueryClient();
}
}
class QueryClient extends Frame
implements Runnable,ActionListener
{ Button connection,send;
TextField inputText;
TextArea showResult;
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。