当前位置:首页 » 操作系统 » servlet数据库连接

servlet数据库连接

发布时间: 2022-06-26 21:47:06

‘壹’ 如何通过Servlet连接数据库并添加数据

set
conn=server.CreateObject("adodb.connection")
conn.open
"driver={microsoft
Access
driver
(*.mdb)};dbq="&server.MapPath("qq.mdb")
这是连接数据库的,qq是数据名
set
rs=server.CreateObject("adodb.recordset")
sql="select
*
from
procts"
rs.open
sql,conn,1,1
这是连接数据表的,连接完成之后方可添加数据
,这里的procts
是数据表的名称

‘贰’ 为什么我的servlet链接不了数据库

//1.调用UserService完成查询
UserService service = new UserServiceImpl();
List<User> users = service.findAll();
在UserListServlet 方法外
使用@Autowired private UserService userService;
或者@Resource private UserService userService;
然后启动项目才可以引入service,因为spring构建bean并注入是在项目启动时注入,当前引入的,无法通过new去声明service;

‘叁’ Servlet中数据库表连接查询

servlet:

String cond = "select * from user_maxiao ";
List list = (new UserInfoDAO()).getUser(cond);
request.setAttribute("list", list);
request.setAttribute("user", user);
request.getRequestDispatcher("/person/personlist.jsp").forward(
request, response);
UserInfoDAO:

public List getUser(String cond) {
List list = new ArrayList();
........
return list;
}
转载:https://..com/question/549836670.html

‘肆’ 怎么在servlet中连接数据库

// 纯java方法,建立连接,数据库名称为:TestDB
public Connection getConnection() {
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB";
java.sql.Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String user = "sa";
String pwd = "123456";
con = java.sql.DriverManager.getConnection(url, user, pwd);
}
catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
以上代码是创建数据库连接的语句,在具体应用时调用这个方法就可以了。

‘伍’ 用servlet 连接和插入数据库

package com.jsp.Database;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
public class JDBCServlet extends HttpServlet
{
/* 连接数据库属性*/
String driver;
String password,url,user;
/*处理doGet */public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
try
{
Connection con=getConnection();
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from user_info");
while(rst.next())
{
out.println(rst.getString("userid"));
out.println("<br>");

}
rst.close();
stmt.close();
con.close();

}
catch(SQLException e)
{
e.printStackTrace();
}
}
/*处理doPost请求*/
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
doGet(request,response);
}public void init()throws ServletException
{
driver=getInitParameter("DRIVER");
password=getInitParameter("PASSWORD");
url=getInitParameter("URL");
user=getInitParameter("USER");
}
private Connection getConnection()
{
Connection con=null;
try
{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);

}
catch(Exception e )
{
e.printStackTrace();
}

return con;

}

} ======== web.xml文件配置========
<servlet>
<servlet-name>JDBCServlet</servlet-name>
<servlet-class>com.jsp.Database.JDBCServlet</servlet-class>
<init-param>
<param-name>DRIVER</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</init-param><init-param>
<param-name>URL</param-name>
<param-value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev</param-value>
</init-param><init-param>
<param-name>USER</param-name>
<param-value>sa</param-value>
</init-param>
<init-param><param-name>PASSWORD</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JDBCServlet</servlet-name>
<url-pattern>/init_servlet</url-pattern>
</servlet-mapping>WEB.XML在你的tomcat/webapp/你的目录/WEB-INF/下..
或者复制tomcat/webapp/ROOT/WEB-INF/下的,,做一下修改就行了.

‘陆’ 怎样用servlet进行对数据库的操作

1、首先明确一点,servlet就是一个继承自javax.servlet.http.HttpServlet的类。

2、范例:使用servlet连接数据库

packagecom.oracle.project.servlet;

importjava.util.*;
importjava.io.*;
importjava.sql.*;

importjavax.servlet.*;
importjavax.servlet.http.*;
importjavax.servlet.annotation.*;

@WebServlet("/TestServlet")

{
="oracle.jdbc.driver.OracleDriver";
publicstaticfinalStringDBURL="jdbc:oracle:thin:@localhost:1521:ORACLEDB";
publicstaticfinalStringDBUSER="scott";
="tiger";
privateConnectionconn=null;

publicConnectiongetConnection(){
try{
if(this.conn==null){
Class.forName(DBDRIVER);
this.conn=DriverManager.getConnection(DBURL,DBUSER,PASSWORLD);
}
}catch(Exceptione){
e.printStackTrace();
}
returnconn;
}

@Override
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
PrintWriterout=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>"+this.getConnection()+"</h1>");
out.println("</body>");
out.println("</html>");
}
@Override
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
this.doGet(request,response);
}
}

‘柒’  java web中servlet类中连接数据库问题

<body>
<form action="servlet/Login" method="post">
用户名:<input type="text" name="username"> <br/>
密 码 :<input type="keyword" name="keyword"><br/>
学 号:<input type="num" name="num"><br/>
<input type="submit" value="注册">

</form>
</body>

下面是获取表单信息,并保存到数据库中的servlet,注意获取信息时名字要和jsp提交的信息一致,
response.setContentType("text/html;charset=UTF-8");
//获取输入信息
String member = request.getParameter("username");
String keyword = request.getParameter("keyword");
String num = request.getParameter("num");

//连接数据库
String url = "jdbc:mysql://localhost:3306/thecheck";
String user="root";
String password = "19960104";

try {
Class.forName("com.mysql.jdbc.Driver");

try {
java.sql.Connection conn = DriverManager.getConnection(url, user, password);
java.sql.Statement st = conn.createStatement();
String sql = "insert into student values("+"'"+member+"',"+"'"+keyword+"',"+"'"+num+"',"+"0)";
//System.out.println(sql);
st.executeUpdate(sql);

} catch (SQLException e) {

e.printStackTrace();
}

} catch (ClassNotFoundException e) {

e.printStackTrace();
}

}

其中还要注意的是写的输入语句要和数据库中的sql语句对应,否则也会出现错误。

‘捌’ 怎样用servlet连接数据库

public class ConnSql{
private static Connection conn;
/**
* 数据库链接静态代码块
*/
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//连接本地sql2005 String url="jdbc:sqlserver://192.168.1.111:1433;databaseName=YITAC";
//连接本机sql2005 String url="jdbc:sqlserver://localhost:1433;databaseName=YITAC";
conn = DriverManager.getConnection(url, "sa", "123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
return conn;
}
}
数据库炸包:sqljdbc4.jar

热点内容
福建社保银行卡初始密码是多少 发布:2024-11-15 11:47:40 浏览:911
游戏多开用什么配置 发布:2024-11-15 11:46:51 浏览:729
管理java版本 发布:2024-11-15 11:44:03 浏览:629
ndk编译的程序如何执行 发布:2024-11-15 11:43:18 浏览:626
轻应用服务器适合搭建网站吗 发布:2024-11-15 11:36:08 浏览:246
c语言的百分号 发布:2024-11-15 11:34:24 浏览:31
一加五安卓8什么时候推送 发布:2024-11-15 11:19:40 浏览:854
暗影骑士擎有哪些配置 发布:2024-11-15 11:13:46 浏览:598
方舟主机专用服务器是什么意思 发布:2024-11-15 11:12:23 浏览:8
创维最早的服务器是什么 发布:2024-11-15 11:11:35 浏览:864