jdbc数据库操作

如何利用jdbc操作mysql数据库,自行下载安装mysql,下载jdbc的jar,写程序时导入jar包
//
第一步
先在数据库中建立一个表datas
mysql> create table datas(
    -> id int unsigned not null auto_increment primary key,
    -> name char(8) not null,
    -> sex char(8) not null,
    -> age char(3) not null
    -> );
//
//
第二步
运行Java中的程序
//
//Datas.java
package Test1;

public class Datas {
private String id;
private String name;
private String sex;
private String age;
Datas(String name,String sex,String age)
{
this.id=null;	
this.name=name;
this.sex=sex;
this.age=age;
}
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getAge() {
	return age;
}
public void setAge(String age) {
	this.age = age;
}
}

//test1.java
package Test1;

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

import com.mysql.cj.jdbc.JdbcConnection;

public class test1 {

	public static void main(String[] args){
		// TODO Auto-generated method stub
		getAll();
		insert(new Datas("acd","male","12"));
		getAll();
		update(new Datas("acd","","7"));
		getAll();
		delete("acd");
		getAll();
	}
//数据库连接
	private static Connection getConn(){
		String url="jdbc:mysql://localhost:3306/yuec?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
		String user="root";
		String password="5499";
		Connection conn=null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn=(Connection)DriverManager.getConnection(url,user,password);
		} catch (SQLException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
//往表中输入一条数据
	private static int insert(Datas data)
	{
		Connection conn=getConn();
		int i=0;
		String sql="insert into datas (name,sex,age) values(?,?,?)";
		PreparedStatement pstmt;
		try {
			pstmt=(PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1, data.getName());
			pstmt.setString(2, data.getSex());
			pstmt.setString(3, data.getAge());
			i=pstmt.executeUpdate();
			pstmt.close();
			conn.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}
//变更指定name的age键值
	private static int update(Datas data) {
		Connection conn=getConn();
		int i=0;
		String sql="update datas set age='"+data.getAge()+"'where name='"+data.getName()+"'";
		PreparedStatement pstmt;		
		try {
			pstmt=(PreparedStatement)conn.prepareStatement(sql);
			i=pstmt.executeUpdate();
			System.out.println("result:"+i);
			pstmt.close();
			conn.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}
//输出整个表"select * from datas;"
	private static Integer getAll () {
		Connection conn=getConn();
		String sql="select * from datas";
		PreparedStatement pstmt;
		try {
			pstmt=(PreparedStatement)conn.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			int col=rs.getMetaData().getColumnCount();
			System.out.println("======================");
			while(rs.next())
			{
				for(int i=1;i<=col;i++) {
					System.out.print(rs.getString(i)+"\t");
				if((i==2)&&(rs.getString(i).length()<8)) {
					System.out.print("\t");
				}
			}
				System.out.println("");
		}
			System.out.println("========================");
	}catch (SQLException e) {
		e.printStackTrace();
	}
		return null;
}
//删除表中指定name的数据
	private static int delete(String name) {
		Connection conn=getConn();
		int i=0;
		String sql="delete from datas where Name='"+name+"'";
		PreparedStatement pstmt;
		try {
			pstmt=(PreparedStatement)conn.prepareStatement(sql);
			i=pstmt.executeUpdate();
			System.out.println("result:"+i);
			pstmt.close();
			conn.close();
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return i;
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里东君~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值