JAVA数据库编程(JDBC技术)-入门笔记(四)
atch()
清空此Statement对象的当前SQL命令列表
executeBatch()
将一批命令提交给数据库执行,如果全部命令执行成功,则返回更新计数组的数组。
executeUpdate()
执行给定的SQL语句,该语句可以为INSERT,UPDATE或者DELETE
addBatch(String sql)
将给定的SQL命令添加到Satement对象的当前命令列表中。
close()
释放Statement实例占用的数据库和JDBC资源
接着我们在写一个删除这次的演示就搞完,删除代码如下:
package myJava.jdbc;
import java.sql.*;
public class SelectQuery {
Connection conn;
//创建一个返回值为Connection的方法
public Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动加载成功");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=DB_ShowData","sa","123456");
if(conn!=null){
System.out.println("数据库连接成功");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回Connection对象
return conn;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SelectQuery getcon = new SelectQuery();
getcon.getConnection();
//查询
/** String sql = "SELECT * FROM [dbo].[User]";
try {
PreparedStatement Ps = getcon.conn.prepareStatement(sql);
ResultSet Rs = Ps.executeQuery();
while(Rs.next()){
String name = Rs.getString("Name");
String password = Rs.getString("Password");
System.out.println(name);
System.out.println(password);
}
Rs.close();
Ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**/
//添加
/** try {
Statement statement = getcon.conn.createStatement();
int count = statement.executeUpdate("INSERT INTO [dbo].[User] VALUES ('Superman','001002')");
getcon.conn.close();
System.out.println("添加行数为"+count);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**/
//删除
try {
Statement statement = getcon.conn.createStatement();
int count = statement.executeUpdate("DELETE FROM [dbo].[User] WHERE Name LIKE 'Superman'");
getcon.conn.close();
System.out.println("成功删除行数"+count);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}