pageEncoding="utf-8"%>

Insert title here

Statement stmt=conn.createStatement();

ResultSet rs = stmt.executeQuery("select * from song_title"); //执行SQL语句

while(rs.next()){

out.println("
:"+rs.getInt(1)+"
分类:"+rs.getString(2));

%>

}

rs.close();

stmt.close();

%>

String driverClass="com.mysql.jdbc.Driver";

String url="jdbc:mysql://localhost:3306/jsp_mysql?user=root&password=root&useUnicode=true&characterEncoding=utf-8";

String username = "root";

String password = "";

Class.forName(driverClass); // 加载数据库驱动

Connection conn=DriverManager.getConnection(url, username, password); //建立连接

//静态的SQL语句实现

out.println("Statement对象执行静态的SQL语句实现");

out.println("");

123456789101112131415161718192021222324252627282930313233343536373839

静态的SQL语句实现

查询数据

ResultSet rs = stmt.executeQuery("select * from tb_user"); //执行SQL语句

while(rs.next()){

out.println("
用户名:"+rs.getString(2)+" 密码:"+rs.getString(3));

}

rs.close();

stmt.close();

out.println("

添加操作
");

1234567

添加操作

Statement addstmt=conn.createStatement();

int addrtn=addstmt.executeUpdate("insert into tb_user (username,password) values( '小明','188979230692')");

123

修改操作

Statement updatestmt=conn.createStatement();

int updatertn=updatestmt.executeUpdate(" update tb_user set username='Tom',password='1234admin' where username='小红'");

123

删除操作

Statement delstmt=conn.createStatement();

int delrtn=delstmt.executeUpdate("delete from tb_user where username='小明'");

out.println("

PreparedStatement对象执行动态的SQL语句实现
");

12345

动态的SQL语句实现

动态查询数据

PreparedStatement addpStmt=conn.prepareStatement("insert into tb_user (username,password) values (?,?)");

addpStmt.setString(1, "小红");

addpStmt.setString(2, "1201245015415");

int addrtnPrep=addpStmt.executeUpdate();

12345

动态修改数据

PreparedStatement updatepStmt=conn.prepareStatement("update tb_user set username=? , password=? where username=?");

updatepStmt.setString(1, "小妹");

updatepStmt.setString(2, "QQ298942481");

updatepStmt.setString(3, "Tom");

int updateprtn=updatepStmt.executeUpdate();

123456

动态删除数据

PreparedStatement delpStmt=conn.prepareStatement("delete from tb_user where username=?");

delpStmt.setString(1, "user2");

int delrtn=delpStmt.executeUpdate();

1234

conn.close();

%>

相关文章