java mysql plus

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

public class helloworld {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata?useUnicode=true&characterEncoding=utf-8", "root", "");
		System.out.println(con);
		
		
		String sql_in = "insert into student values(null, ?, ?)";
		PreparedStatement pst_in = con.prepareStatement(sql_in);
		pst_in.setString(1, "灑錒魤");
		pst_in.setInt(2, 56);
		int exi = pst_in.executeUpdate();
		if(exi == 1) System.out.println("insert success");
		
		
		String sql_de = "delete from student where id=?";
		PreparedStatement pst_de = con.prepareStatement(sql_de);
		pst_de.setInt(1, 13);
		int exd = pst_de.executeUpdate();
		if(exd == 1) System.out.println("delete success");
		
		
		String sql_ud = "update student set name=?,age=? where id=?";
		PreparedStatement pst_ud = con.prepareStatement(sql_ud);
		pst_ud.setString(1, "xiaohong");
		pst_ud.setInt(2, 99);
		pst_ud.setInt(3, 8);
		int exu = pst_ud.executeUpdate();
		if(exu == 1) System.out.println("update success");
		
		String sql = "select * from student";
		PreparedStatement pst = con.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next())
		{
			int id = rs.getInt(1);
			String name = rs.getString(2);
			int age = rs.getInt(3);
			System.out.printf("%d %s %d\n", id, name, age);
		}
		rs.close();
		pst.close();
		con.close();
	}

}

你可能感兴趣的:(Java)