1. 程式人生 > >11/28課程實踐

11/28課程實踐

jdbc classname ktr highlight ces conn http ring 文件

一:設計思想

鏈接數據庫的步驟就是:

* 1 加載驅動

Class.forName("com.mysql.jdbc.Driver").newInstance();

String classname = "root";
String classteacher = "root";
String classspace = "jdbc:mysql://localhost:3306/class";
Connection connection = null;


* 2 創建鏈接對象

connection = DriverManager.getConnection(classspace,classname,classteacher);


* 3 創建語句傳輸對象

ResultSet resultSet=null;
PreparedStatement preparedStatement = null;
Connection connection = DBUtil.getConnection();


* 4 接收結果集對象

resultSet = preparedStatement.executeQuery();


* 5 遍歷

sql = "insert into t_user(username,password,nickname) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getNickname());
preparedStatement.executeUpdate();


* 6 關閉資源

DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);

連接上數據以後就是要寫jsp文件,在jsp文件中要進行輸入的寫入,在進行提交到數據庫就好了

二:實驗代碼:

package com.jaovo.msg.teach;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import sun.net.www.content.text.plain;
public class shujuku {	
	public void gets(student st) {
		//獲得鏈接對象
		ResultSet resultSet=null;
		PreparedStatement preparedStatement = null;
		Connection connection = DBUtil.getConnection();
		//準備sql語句
		String sql = "insert into t_class(classname,classteacher,classspace) values(?,?,?)";
			try {
				preparedStatement = connection.prepareStatement(sql);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				preparedStatement.setString(1, st.getClassname());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				preparedStatement.setString(2, st.getClassteacher());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				preparedStatement.setString(3, st.getClassspace());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				preparedStatement.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			//關閉資源
			DBUtil.close(resultSet);
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
	
		
}




	
	

  

package com.jaovo.msg.teach;

public class student {
	
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	private String  classteacher;
	private String  classname;
	private String  classspace;
	
	public String getClassteacher() {
		return classteacher;
	}
	public void setClassteacher(String classteacher) {
		this.classteacher = classteacher;
	}
	public String getClassname() {
		return classname;
	}
	public void setClassname(String classname) {
		this.classname = classname;
	}
	public String getClassspace() {
		return classspace;
	}
	public void setClassspace(String classspace) {
		this.classspace = classspace;
	}

	
}

  

package com.jaovo.msg.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
	public  static  Connection getConnection() {
		try {
			//1 加載驅動
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String classname = "root";
		String classteacher = "root";
		String classspace = "jdbc:mysql://localhost:3306/class";
		Connection connection = null;
		try {
			//2 創建鏈接對象connection
			 connection = DriverManager.getConnection(classspace,classname,classteacher);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}
	
	//關閉資源的方法
	public static void close(Connection connection ) {
		try {
			if (connection != null) {
				connection.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement preparedStatement ) {
		try {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void close(ResultSet resultSet ) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

  

<%@page import="com.jaovo.msg.teach.shujuku"%>
<%@page import="com.jaovo.msg.teach.student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	//接收客戶端傳遞過來的參數
	String classname = request.getParameter("classname");
		if(classname==null||classname=="")
		{
			System.out.println("課程名字不得為空");
		}
	String classteacher = request.getParameter("classteacher");
		if(classteacher==null||classteacher=="")
		{
			System.out.println("課程老師不得為空");
		}
	String classspace = request.getParameter("classspace");
		if(classspace==null||classspace=="")
		{
			System.out.println("課程教室不得為空");
		}
	student st=new student();
	System.out.println(classname);
	st.setClassname(classname);
	st.setClassteacher(classteacher);
	st.setClassspace(classspace);
	shujuku s = new shujuku();
	try{
		s.gets(st);
	}
	catch(NullPointerException q)
	{
		q.printStackTrace();
	}
	%>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>信息添加頁面</title>
</head>
<body>
	<%=request.getAttribute("error") %>
	<form action="add.jsp" method="get">
		<table align="center" border="1" width="500">
			<tr>
				<td> 課程名稱 </td>
				<td>
					<input type="text" name="classname" />
				</td>
			</tr>
				<tr>
    			<td>課程教師</td>
    			<td>
    				<input type="text" name="classteacher" />
    			</td>
    		</tr>
    		<tr>
    			<td>上課教室</td>
    			<td>
    				<input type="text" name="classspace" />
    			</td>
    		</tr>
    		<tr align="center">
    			<td colspan="2">
    				<input type="submit" value="保存" />
    				
    				
    			</td>
    		</tr>
		</table>
	</form>
</body>
</html>

  三:運行結果

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

個人程序總結

技術分享圖片

11/28課程實踐