⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usedriver.java

📁 jsp 应用开发技术光盘 是《jsp应用开发技术》这本书的源代码
💻 JAVA
字号:
package ch13;

import java.sql.*;


public class UseDriver {
	public static void main(String[] args) {
		String url = "jdbc:mysql://localhost/sql_test";
		String userName = "root";
		String password = "root";
		String sql = null;
		Connection conn = null;
		Statement stmt = null;
		try {
			//第一步:加载驱动器
			Class.forName("com.mysql.jdbc.Driver");
		} catch(ClassNotFoundException e) {
			System.err.print("ClassNotFoundException");
		}
		
		try {
            //第二步:调用DriverManager.getConnection静态方法得到数据库连接
			conn = DriverManager.getConnection(url, userName, password);
			//创建Statement语句
			stmt = conn.createStatement();
			sql = "INSERT INTO student " +
			      "VALUES('12', 'zhangjun', 'tianjin', '1981-01-01')";
            //使用Statement语句对象执行SQL语句
			stmt.executeUpdate("DELETE FROM student WHERE stu_id='12'");
			stmt.executeUpdate(sql);
			System.out.println("Insert a row successful!");
		} catch(SQLException e) {
			System.err.println("Insert SQLException");
		} finally {
			//关闭语句和数据库连接
			try {
				stmt.close();
				conn.close();
			} catch(SQLException e) {
				System.err.println("Close SQLException");
			}		
		}
		
	}
}

⌨️ 快捷键说明

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