java对mysql数据库的操作(连接,增删改查)

java对mysql数据库的操作(连接,增删改查)

环境:myeclipse 2014  +  tomcat  +  mysql (Navicat for mysql)

1.  实现查询:

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBChaxun {
	private static final String url = "jdbc:mysql://localhost:3306/web";
	private static final String user = "root";
	private static final String password = "root";

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//1. 加载驱动程序(mysql)
		Class.forName("com.mysql.jdbc.Driver");  
		//2.  获得数据库的连接
		Connection conn = DriverManager.getConnection(url, user, password);
		//3.  通过数据库的连接操作数据库,实现增删改查
		Statement stmt = conn.createStatement();  //创建statement对象
		ResultSet rs = stmt.executeQuery("select name,age from info");  //(1)实现查询功能,并且返回resultset对象
		//4.  获取resultset对象中的数据
		while(rs.next())
		{
			System.out.println(rs.getString("name")+rs.getInt("age"));
		}
	}

}

2.   实现删除:

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBDelete {
	private static final String url = "jdbc:mysql://localhost:3306/web";
	private static final String user = "root";
	private static final String password = "root";

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//1. 加载驱动程序(mysql)
		Class.forName("com.mysql.jdbc.Driver");  
		//2.  获得数据库的连接
		Connection conn = DriverManager.getConnection(url, user, password);
		//3.  通过数据库的连接操作数据库,实现增删改查
		Statement stmt = conn.createStatement();  //创建statement对象
		String sql = "delete from info where name = 'lry' ";
//		stmt.execute(sql); //直接执行插入数据到数据库的表中
		int rows = stmt.executeUpdate(sql);   
		System.out.println("影响的行数是:"+rows);
	}

}

3.  实现增加(插入):
 

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBInsert {
	private static final String url = "jdbc:mysql://localhost:3306/web";
	private static final String user = "root";
	private static final String password = "root";

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//1. 加载驱动程序(mysql)
		Class.forName("com.mysql.jdbc.Driver");  
		//2.  获得数据库的连接
		Connection conn = DriverManager.getConnection(url, user, password);
		//3.  通过数据库的连接操作数据库,实现增删改查
		Statement stmt = conn.createStatement();  //创建statement对象
		String sql = "insert into info values('kjq',21,'男');";  //sql语句
//		stmt.execute(sql); //直接执行插入数据到数据库的表中
		int rows = stmt.executeUpdate(sql);   
		System.out.println("影响的行数是:"+rows);
	}

}

4.  实现修改:

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBChange {
	private static final String url = "jdbc:mysql://localhost:3306/web";
	private static final String user = "root";
	private static final String password = "root";

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//1. 加载驱动程序(mysql)
		Class.forName("com.mysql.jdbc.Driver");  
		//2.  获得数据库的连接
		Connection conn = DriverManager.getConnection(url, user, password);
		//3.  通过数据库的连接操作数据库,实现增删改查
		Statement stmt = conn.createStatement();  //创建statement对象
		String sql = "update info set sex = 'nan' where name = 'kjq'; ";
//		stmt.execute(sql); //直接执行插入数据到数据库的表中
		int rows = stmt.executeUpdate(sql);   
		System.out.println("影响的行数是:"+rows);
	}

}

本篇博客到此结束!

 


版权声明:本文为MyDream8原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。