JAVA通过JDBC操作MySQL数据库(三):PreparedStatement接口操作数据库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40558287/article/details/85416913

JAVA通过JDBC操作MySQL数据库(三):PreparedStatement接口操作数据库

Statement接口的问题

在文章JAVA通过JDBC操作MySQL数据库(二):Statement接口操作数据库 中提到过,Statement接口操作数据库是有问题的,所以我们基本上不会使用Statement接口操作数据库,而是使用PreparedStatement接口。

在日常编程中我们需要向数据库中输入和取出的信息一般都是变量,不会跟在JAVA通过JDBC操作MySQL数据库(二):Statement接口操作数据库 中那样直接把内容写在字符串中,如果要在Statement接口中加入变量那就要做字符串拼凑,如下面的代码所示:

public class Mydemo {
	private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
	private static final String DBUSER="root";
	private static final String DBPASSWORD="root";
	public static void main(String[] args) throws Exception {
		String name="sc";
		int age=20;
		String city="nanjing";
		String brief="new student";
		String birthday="1998-5-12";
		Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
		Statement st=connection.createStatement();//产生Statement实例化对象
		String sql=" insert into student values('"+name+"',"+age+",'"+city+"','"+brief+"','"+birthday+"') ";
		//拼凑字符串将变量写进数据库操作语句中
		int len=st.executeUpdate(sql);//执行更新操作
		System.out.println(len);
		connection.close();
	}
}

可以看到这样很复杂,使用PreparedStatement能够便捷的将变量写进SQL语言,还能避免某些特殊符号引起的SQL语法错误。

PreparedStatement接口操作数据库

PreparedStatement和Statement的主要区别是在SQL语言中用占位符 代替变量,再依次设置每个占位符的内容,需要特别注意的是占位符只能用来表示数据,不能用来标识数据库的列名。PreparedStatement的主要内容如下:

  1. Connection产生PreparedStatement接口实例(PreparedStatement prepareStatement(String sql) throws SQLException),sqlJAVA通过JDBC操作MySQL数据库(二):Statement接口操作数据库 中Statement的sql
  2. 更新数据库(int executeUpdate() throws SQLException),返回值同Statement。
  3. 查询数据库(ResultSet executeQuery() throws SQLException),返回值同Statement。
  4. 设置占位符内容(void setString(int parameterIndex,String x) throws SQLException),int parameterIndex 是指占位符的序号,从1算起,String x 就是占位符的内容,类似的还有setInt()setFloat 等。
  5. 在平常的java编程中使用的Date是java.util.Date,如果要把java.util.Date变量写进数据库要把java.util.Date变化为java.sql.Date,通过java.util.Date的getTime() 方法得到long型返回值,再将其传入java.sql.Date的构造函数即可,具体方式见例程。

更新数据库的例程:

public class Mydemo {
	private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
	private static final String DBUSER="root";
	private static final String DBPASSWORD="root";
	public static void main(String[] args) throws Exception {
		String name="cc";
		int age=1;
		String city="luoyang";
		String brief="new student";
		Date birthday=new Date();//java.util.Date
		Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
		String sql=" insert into student values(?,?,?,?,?) ";
		PreparedStatement pst=connection.prepareStatement(sql);//产生PreparedStatement实例化对象		
		pst.setString(1, name); //定义占位符的内容
		pst.setInt(2, age);
		pst.setString(3, city);
		pst.setString(4, brief);	
		pst.setDate(5, new java.sql.Date(birthday.getTime()));//java.util.Date转java.sql.Date
		int len=pst.executeUpdate();//执行更新操作
		System.out.println(len);
		connection.close();
	}
}

查询数据库的例程,找出student表中年龄为20的学生信息:

public class Mydemo {
	private static final String DBURL="jdbc:mysql://localhost:3306/gc? serverTimezone=GMT";
	private static final String DBUSER="root";
	private static final String DBPASSWORD="root";
	public static void main(String[] args) throws Exception {
		Connection connection=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
		String sql=" select name,age,birthday from student where age=? ";
		PreparedStatement pst=connection.prepareStatement(sql);//产生PreparedStatement实例化对象		
		pst.setInt(1, 20); //定义占位符的内容
		ResultSet  result=pst.executeQuery();
		while (result.next()) {
			System.out.println(result.getString(1)+" "+result.getInt(2)+" "+result.getDate(3));
		}
		connection.close();
	}
}

系列文章
JAVA通过JDBC操作MySQL数据库(一):MySQL数据库准备
JAVA通过JDBC操作MySQL数据库(二):Statement接口操作数据库

猜你喜欢

转载自blog.csdn.net/weixin_40558287/article/details/85416913