继续昨天的
删除语句
//调用包
import java.sql.Connection ;
//调用包
import java.sql.DriverManager ;
//调用包
import java.sql.Statement ;
/**
* cheyan2 类
* @author xinjie
*
*/
public class cheyan2{
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/ace" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "root" ;
/**
* main() 主方法
* @param String[] args
* @throws Exception
* return 空
*/
// 所有的异常抛出
public static void main(String args[]) throws Exception {
// 数据库连接
Connection conn = null ;
// 数据库操作
Statement stmt = null ;
// 加载驱动程序
Class.forName(DBDRIVER) ;
//定义变量 SQL的删除代码
String sql = "DELETE FROM users WHERE user_name = 'chenpeng' ;";
//获得连接
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
// 实例化Statement对象
stmt = conn.createStatement() ;
// 执行数据库更新操作
stmt.executeUpdate(sql) ;
// 关闭操作
stmt.close() ;
// 数据库关闭
conn.close() ;
}
}
更新语句
//调用包
import java.sql.Connection ;
//调用包
import java.sql.DriverManager ;
//调用包
import java.sql.Statement ;
/**
* cheyan2 类
* @author xinjie
*
*/
public class shiyan3{
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/ace" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "root" ;
/**
* main() 主方法
* @param String[] args
* @throws Exception
* return 空
*/
// 所有的异常抛出
public static void main(String args[]) throws Exception {
// 数据库连接
Connection conn = null ;
// 数据库操作
Statement stmt = null ;
// 加载驱动程序
Class.forName(DBDRIVER) ;
//定义变量 SQL的更新代码
String sql = "UPDATE users SET user_name = 'chenmimimi' WHERE user_name = 'chenmihua' ;";
//获得连接
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
// 实例化Statement对象
stmt = conn.createStatement() ;
// 执行数据库更新操作
stmt.executeUpdate(sql) ;
// 关闭操作
stmt.close() ;
// 数据库关闭
conn.close() ;
}
}
查询语句
//调用包
import java.sql.Connection ;
//调用包
import java.sql.ResultSet;
//调用包
import java.sql.DriverManager ;
//调用包
import java.sql.Statement ;
/**
* shiyan4 类
* @author xinjie
*
*/
public class shiyan4{
// 定义MySQL的数据库驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/ace" ;
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "root" ;
/**
* main() 方法
* @param String[] args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 数据库连接
Connection conn = null ;
// 数据库连接
Statement stmt = null ;
// 加载驱动程序
Class.forName(DBDRIVER) ;
//定义变量 SQL的查询代码
String sql = "SELECT * FROM users;";
//获得连接
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
// 实例化Statement对象
stmt = conn.createStatement() ;
// 执行数据库查找操作
ResultSet rs = stmt.executeQuery(sql);
//循环
while(rs.next()){
//输出
System.out.println(rs.getString("user_name"));
}
// 关闭操作
stmt.close() ;
// 数据库关闭
conn.close() ;
}
}
11月28号(第26天的学习)
猜你喜欢
转载自xjwolaile.iteye.com/blog/1736533
今日推荐
周排行