JDBC编程之数据库基本操作

一、读取数据库

package com.djx.jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.sun.org.apache.regexp.internal.recompile;

public class JDBETest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String sql="select * from tbl_user";
		Connection conn=null;
		Statement statement=null;
		ResultSet set=null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root", "1234");
				statement=(Statement) conn.createStatement();
				set=statement.executeQuery(sql);
				while(set.next()){
					System.out.print(set.getInt("id")+" ");
					System.out.print(set.getString("name")+" ");
					System.out.print(set.getString("password")+" ");
					System.out.print(set.getString("email")+" ");
					System.out.println();
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				set.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}

二、对表进行增加,修改,删除操作

package com.djx.jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.sun.org.apache.regexp.internal.recompile;

public class JDBETest {
	public static Connection getConnection() {
		Connection conn=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db?autoReconnect=true&useSSL=false","root","1234");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	public static void insert() {
		Connection conn=getConnection();
		String sql="insert into tbl_user(name,password,email) values('Tom','1234','tom@163.com')";
		int count;
		try {
			Statement statement=(Statement)conn.createStatement();
			count = statement.executeUpdate(sql);
			System.out.println("向用户表中插入"+count+"条记录");
		    statement.close();
		    conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void update() {
		Connection conn=getConnection();
		String sql="update  tbl_user set email='tom@qq.com' where name='Tom'";
		int count;
		try {
			Statement statement=(Statement)conn.createStatement();
			count = statement.executeUpdate(sql);
			System.out.println("用户表中更新"+count+"条记录");
		    statement.close();
		    conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void delete() {
		Connection conn=getConnection();
		String sql="delete from  tbl_user  where name='Tom'";
		int count;
		try {
			Statement statement=(Statement)conn.createStatement();
			count = statement.executeUpdate(sql);
			System.out.println("用户表中更新"+count+"条记录");
		    statement.close();
		    conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		insert();
		update();
		delete();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值