usestatement.java

来自「nanjing university cs 的java课件。 对新手很有用。付」· Java 代码 · 共 66 行

JAVA
66
字号
/*
 * Created on 2004-8-9
 * 
 * TODO To change the template for this generated file go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
package com.sanshang.jdbc;

/*
 * Copyright 2003 Sun Microsystems, Inc. ALL RIGHTS RESERVED. Use of this
 * software is authorized pursuant to the terms of the license found at
 * http://developer.java.sun.com/berkeley_license.html.
 */

import java.sql.*;

public class UseStatement {

	public static void main(String args[]) {
		Connection con = null;
		Statement stmt;
		String query = "select * from user";

		try {
			// step 1 load driver
			Class.forName(DBInfo.driver);

		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
			// step 2 get connection
			con = DriverManager.getConnection(DBInfo.dbURI, DBInfo.user,
					DBInfo.password);
			// step 3 create statement
			stmt = con.createStatement();
			// step 4 execute query
			stmt.executeUpdate("insert into user (Name,Password,Role) "
					+ "values('wang', '123', 11)");
			ResultSet rs = stmt.executeQuery(query);
			// step 5 Process the results
			System.out.println("ID   Name");
			while (rs.next()) {
				System.out.println(rs.getString("ID") + "   "
						+ rs.getString("Name"));
			}

			stmt.close();

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?