<p>PHP、Java、C++大神教你建站基础代码</p><p>在当今这个信息化时代,网站已经成为了企业和个人展示自己的窗口,掌握一门或多门编程语言,可以帮助我们快速搭建网站,本文将分别介绍PHP、Java和C++三种编程语言在建站过程中的基本代码。</p><p>一、PHP基础代码</p><p>1. 连接数据库</p><pre><code><?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
?></code></pre><p>2. 获取用户输入数据并插入数据库</p><pre><code><?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$sql = "INSERT INTO messages (name, email, message) VALUES ('$name', '$email', '$message')";
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?></code></pre><p>3. 从数据库中读取数据并显示在网页上</p><pre><code><?php
$sql = "SELECT id, name, email FROM messages";
$result = $conn->query($sql);
?></code></pre><!DOCTYPE html><html><head><meta charset="utf-8"><title>Messages</title></head><body><h1>Messages</h1><?php if ($result->num_rows > 0): ?><form action="" method="post"> Name:<input type="text" name="name"><br> E-mail:<input type="text" name="email"><br> Message:<textarea name="message"></textarea><br><input type="submit" value="Submit"></form><?php while($row = $result->fetch_assoc()): ?><p><strong><?php echo $row["name"]; ?></strong> (<?php echo $row["email"]; ?>)</p><?php endwhile; else: echo "0 results"; endif; ?></body></html>```
二、Java基础代码(使用Servlet处理表单提交)</p><p>1. 建立数据库连接(这里以MySQL为例)</p><pre><code>import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbcp2.BasicDataSource;
public class MessageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection connection;
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try{connection = DriverManager.getConnection("jdbc:mysql://localhost/myDB", "username", "password");}catch(SQLException e){throw new RuntimeException(e);}}@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{String name = request.getParameter("name");String email = request.getParameter("email");String message = request.getParameter("message");try{Statement statement = connection.createStatement();String sql = "INSERT INTO messages (name, email, message) VALUES ('" + name + "','" + email + "','" + message + "')";statement.executeUpdate(sql);response.sendRedirect("messages.jsp");}catch(SQLException e){throw new RuntimeException(e);}}protected void destroy() throws Exception{connection.close();}}``</p><p>2. 在JSP页面中显示数据库中的数据(messages.jsp)</p>
`jsp%><!DOCTYPE html><html><head>Messages</head><body><h1>Messages</h1><%@ page import="java.sql.*" %><%@ page import="javax.naming.*" %><%@ page import="javax.sql.*" %><%@ page import="javax.servlet.*" %><%@ page import="javax.servlet.http.*" %><%@ page import="javax.servlet.jsp.*" %><%@ page import="javax.naming.InitialContext" %><% InitialContext ctx = new InitialContext(); Connection conn = (Connection)ctx.lookup("java:comp/env/jdbc/myDB"); Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM messages"); while(resultSet.next()){%><%= resultSet["id"] %><%= resultSet["name"] %><%= resultSet["email"] %><%= resultSet["message"] %><br><%}%></body></html>
``
还没有评论,来说两句吧...