private static int counter
private String no
private String name
private String sex
public Student(String no,String name,String sex) {
this.no=no
this.name=name
this.sex=sex
counter++
}
public void printStudentInfo(){
System.out.println("学生学号:"+no)
System.out.println("学生姓名:"+name)
System.out.println("学生性别:"+sex)
}
public static void printCounter(){
System.out.println("当前学生数量:"+counter)
}
}
public class Teacher {
private static Integer initNo=null
private String no
private String name
private String grade
public Teacher(String name,String grade) {
if(initNo==null){
initNo=(int)(Math.floor(Math.random()*1000)+1)
this.no=String.valueOf(initNo)
}else{
this.no=String.valueOf(++initNo)
}
this.name=name
this.grade=grade
}
public void printNo(){
System.out.println("教师工号:"+this.no)
}
}
public class Program {
public static void main(String[] args) {
Student student=new Student("001","小明","男")
student.printStudentInfo()
Student.printCounter()
student=new Student("002","小美","女")
student.printStudentInfo()
Student.printCounter()
Teacher teacher=new Teacher("张老师","六年级二班")
teacher.printNo()
teacher=new Teacher("李老师","六年级三班")
teacher.printNo()
teacher=new Teacher("王老师","六年级四班")
teacher.printNo()
}
}
运行的一种结果:
以下是使用JavaWeb编程技术编写程序使用表单提交数据修改和查询数据库使用PreparedStatement对象的示例代码:java
Copy code
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class MyServlet extends HttpServlet {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase"
private static final String USERNAME = "root"
private static final String PASSWORD = "mypassword"
// 处理 GET 请求
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 查询数据库
Connection conn = null
PreparedStatement stmt = null
ResultSet rs = null
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver")
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)
// 创建 PreparedStatement 对象
stmt = conn.prepareStatement("SELECT * FROM mytable WHERE id = ?")
// 设置查询参数
stmt.setInt(1, Integer.parseInt(request.getParameter("id")))
// 执行查询
rs = stmt.executeQuery()
// 处理查询结果
while (rs.next()) {
// 读取数据
int id = rs.getInt("id")
String name = rs.getString("name")
int age = rs.getInt("age")
// 输出结果
response.getWriter().println("ID: " + id + ", Name: " + name + ", Age: " + age)
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace()
} finally {
// 关闭资源
if (rs != null) {
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if (stmt != null) {
try {
stmt.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if (conn != null) {
try {
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
}
// 处理 POST 请求
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 修改数据库
Connection conn = null
PreparedStatement stmt = null
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver")
// 获取数据库连接
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)
// 创建 PreparedStatement 对象
stmt = conn.prepareStatement("UPDATE mytable SET name = ?, age = ? WHERE id = ?")
// 设置更新参数
stmt.setString(1, request.getParameter("name"))
stmt.setInt(2, Integer.parseInt(request.getParameter("age")))
stmt.setInt(3, Integer.parseInt(request.getParameter("id")))
// 执行更新
int count = stmt.executeUpdate()
// 输出结果
response.getWriter().println(count + " row(s) updated.")
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace()
} finally {
// 关闭资源
if (stmt != null) {
try {
stmt.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if (conn != null) {
try {
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
}
}
以下是使用JavaWeb编程技术编写程序使用表单提交数据修改和查询数据库使用PreparedStatement对象的示例代码所对应的输出结果。
当使用 GET 请求访问 /myServlet?id=1 时,输出查询结果:
ID: 1, Name: Alice, Age: 20
当使用 POST 请求提交表单数据 id=1&name=Bob&age=25 时,输出更新结果:
1 row(s) updated.
注:以上输出结果仅供参考,实际输出结果取决于数据库中的数据和执行的 *** 作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)