欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第六章 JDBC

程序员文章站 2022-03-07 11:34:00
...

1,在MySQL创建一个student表,添加数据,然后在控制台输出目前表中记录的总数

package zuoye.bdqn.demo;

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


public class Student {
public static void main(String[] args) {
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye?useUnicode=true&characterEncoding=utf-8","root","520");
		pstmt=conn.prepareStatement("insert into student (id,name) values (?,?)");
		pstmt.setInt(1, 1);
		pstmt.setString(2,"黄志豪");
		pstmt.executeUpdate();
		System.out.println("插入成功!");
		rs=pstmt.executeQuery("select count(*)from student");
		while(rs.next()) {
			System.out.println("表*有"+rs.getInt(1)+"条记录");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(pstmt!=null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
}

2,查询student表所有记录,并在控制台输出数据

package zuoye.bdqn.demo;

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

public class student1 {
		public static void main(String[] args) {
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye","root","520");
		st= conn.createStatement();
		rs=st.executeQuery("select * from student");
		while(rs.next()) {
        System.out.println("序号:"+rs.getInt("id")+"姓名:"+rs.getString("name"));
		            }
		        } catch (Exception e) {
		e.printStackTrace();
		} finally {
		if(conn!=null) {
        try {
		conn.close();
		} catch (SQLException e) {
          e.printStackTrace();
		                }
		            }
}
		    }
		}

3,宠物主人根据控制台输入用户名和密码,如果输入正确,输出用户登录成功,否则输出用户登录失败

package zuoye.bdqn.demo;

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

public class student2 {
   public static void main(String[] args) {
	 Connection conn=null;
	 PreparedStatement pstmt=null;
	 ResultSet rs=null;
	 Scanner in=new Scanner(System.in);
	 System.out.println("\t宠物主人登录");
	 System.out.println("请输入姓名:");
	 String name=in.next();
	 System.out.println("请输入密码:");
	 String password=in.next();
	 try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 try {
		conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye?useUnicode=true&characterEncoding=utf-8","root","520");
		String sql="select *from master where name=?and password=?";
		pstmt=conn.prepareStatement(sql);
		pstmt.setString(1, name);
		pstmt.setString(2, password);
		rs=pstmt.executeQuery();
		if(rs.next()) {
			System.out.println("登录成功!");
		}else {
			System.out.println("登录失败!");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		}
}