
2.导入sql语句。包含表结构,数据等。
3.导入所需要的jar包。
4.准备容器部署项目,如tomcat。
5.将项目部署到tomcat中,运行项目。
哎 我给你最简单的例子两个简单的jsp页面,数据库连接(我给你的是mysql数据库连接示例,后面附sqlserver数据库连接部分关键代码)
首先是 获取值页面My.jsp 源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath()
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'My.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="Hp.jsp">
name:<input name="name" value="" type="text"></br>
password:<input name="password" value="" type="text"></br>
<input type="submit" value="button">
</form>
</body>
</html>
处理页面 Hp.jsp 源码:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath()
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Hp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Connection con = null
Statement stm = null
String url = "jdbc:mysql://localhost:3306/数据名称"//数据库名称就是你的数据库名字
String driver = "com.mysql.jdbc.Driver"//驱动类位置
String username = "root" //数据库登录名称,此处写上你的用户名称
String pwd = "root" //数据库登录密码,此处写上你的登录密码
try
{
Class.forName(driver)
con = DriverManager.getConnection(url, username, pwd)//创建Connection连接对象
stm = con.createStatement() //创建Statement 命令执行对象
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
String name=request.getParameter("name") //获取传过来的名称
String password=request.getParameter("password")//获取传过来的密码
String sql="insert into user(name,password) values("+name+","+password+")"//数据库添加一条记录sql语句
int temp=stm.executeUpdate(sql)
if(temp>0)
{
out.print("添加成功")
}
else
{
out.print("添加失败")
}
//关闭数据库连接
stm.close()
con.close()
%>
</body>
</html>
注意 连接不同数据库要导入不同的数据库驱动包 你要导入才行啊
附 sqlserver数据库连接 部分关键代码:
private static Connection con = null
private static Statement stm = null
private static String url = "jdbc:microsoft:sqlserver://localhost:1433DatabaseName=数据库名称"
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"//与mysql有所不同
private static String username = "sa"//默认用户
private static String pwd = "123"//密码
static {
try {
Class.forName(driver)
con = DriverManager.getConnection(url, username, pwd)
System.out.print("连接成功!")
stm = con.createStatement()
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
代码如下:
建立连接
package Test
import java.sql.*
public class ConnectionManager {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver" //定义常量(下同)
private static final String DATABASE="jdbc:sqlserver://localhost:1433DatabaseName=news"
private static final String DATABASE_USER = "sa"
private static final String DATAVSES_PASS = "123456"
public static Connection getConnection(){
Connection conn = null
try {
Class.forName(DRIVER_CLASS)
conn = DriverManager.getConnection(DATABASE,DATABASE_USER,DATAVSES_PASS)
} catch (Exception e) {
e.printStackTrace()
}
return conn
}
public static void clossConnection (Connection connection){ //释放资源(下同)
try {
if (connection !=null &&!connection.isClosed()){
connection.close()
}
} catch (SQLException e) {
e.printStackTrace()
}
}
public static void closeResultSet(ResultSet rs){
try {
if (rs!=null){
rs.close()
rs = null
}
} catch (Exception e) {
e.printStackTrace()
}
}
public static void closeStatement(PreparedStatement pstmt) {
try {
if (pstmt != null){
pstmt.close()
pstmt = null
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
建立实体类
package Test
import java.util.Date
public class FirestLevelTitle {
private int id // ID
private String titleName//名称
private String creator //创建者
private Date createTime //创建时间
public FirestLevelTitle(int id ,String titleName ,String creator,Date createTime){
this.id = id
this.titleName = titleName
this.creator = creator
this.createTime = createTime
}
public Date getCreateTime() {
return createTime
}
public void setCreateTime(Date createTime) {
this.createTime = createTime
}
public String getCreator() {
return creator
}
public void setCreator(String creator) {
this.creator = creator
}
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
public String getTitleName() {
return titleName
}
public void setTitleName(String titleName) {
this.titleName = titleName
}
}
通过jdbc 访问数据库
package Test
import java.sql.*
import java.util.ArrayList
import java.util.List
public class FirestLeveTitleDbOpereation {
public List getAllFirestLeveTitleList(){ //返回FirestLevelTitle对象的集合
List list = new ArrayList()//定义数组
Connection conn = null
PreparedStatement pstmt = null
ResultSet rs = null
try {
conn = ConnectionManager.getConnection() //建立连接
String sql = "select * from FirstLevelTitle order by CreatorTime desc" //sql语句
pstmt = conn.prepareStatement(sql) //PreparedStatement 语句
rs = pstmt.executeQuery() //查询结果集
while (rs.next()) { //处理结果
int id = rs.getInt("id")
String title = rs.getString("TitleName")
String createw = rs.getString("Creator")
Date time = rs.getDate("CreatorTime")
//把个属性封装到FirestLevelTitle 对象中
FirestLevelTitle fTitle = new FirestLevelTitle(id,title,createw,time)
list.add(fTitle) //添加到集合中
}
} catch (Exception e) {
e.printStackTrace()
}
finally{ //释放资源
ConnectionManager.closeResultSet(rs)
ConnectionManager.closeStatement(pstmt)
ConnectionManager.clossConnection(conn)
}
return list
}
}
jsp页面
<%@ page language="java" pageEncoding="GBK"
import="java.util.* ,Test.*" //添加引用
%>
<html>
<head>
</head>
<body>
<%
FirestLeveTitleDbOpereation first = new FirestLeveTitleDbOpereation()
List list = first.getAllFirestLeveTitleList()
if (list.size()!=0){
for(int i = 0i <list.size()i++){ //循环遍历
FirestLevelTitle fTitle = (FirestLevelTitle)list.get(i)
%>
<td>
表名是: <%=fTitle.getTitleName()%><br/>
id是:<%=fTitle.getId() %><br/>
时间是:<%=fTitle.getCreateTime() %><br/>
</td>
<%
}
}
%>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)