使用jsp在mysql数据库中插入值 [英] insert value in mysql database using jsp
本文介绍了使用jsp在mysql数据库中插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我在jsp eclipse工作。现在我创建了与mysql数据库的连接。但我无法在数据库中插入值。请给出解决方案。我的代码如下:
Now i am working in jsp eclipse. Now i created connection to mysql database. but i can't insert value in database. please give the solution. My code is as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page import="com.Sterling.corejava.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Information</title>
<body>
<%
// get parameters from the request
String LibraryId = request.getParameter("LibraryId");
String LibraryName= request.getParameter("LibraryName");
String LibraryPhno= request.getParameter("LibraryPhno");
String LibraryEmail= request.getParameter("LibraryEmail");
String LibraryAddr= request.getParameter("LibraryAddr");
String FacultyId= request.getParameter("FacultyId");
String Password= request.getParameter("Password");
// declare a connection by using Connection interface
Connection con = null;
Statement stat=null;
try {
String connectionURL = "jdbc:mysql://localhost:3306/libraydatabase";
Class.forName("com.mysql.jdbc.Driver").newInstance ();
con = DriverManager.getConnection(connectionURL, "root", "root123");
out.println("Connection Opened to database.");
stat=con.createStatement();
//String InsertQuery="Select * from tbllibrarian";
//System.out.println("shgfsxv");
String InsertQuery="insert into tbllibrarian(LibraryId,LibraryName,LibraryPhno,LibraryEmail,LibraryAddr,Password) values(11,'weryty',1234,'asdfr@gmail.com','Baramati','****');";
//System.out.println("megha");
try
{
stat.executeUpdate(InsertQuery);
out.println("Record Inserted to database successfully.");
}
catch(SQLException e)
{
out.println("Unable to Insert Record to database.");
}
}
catch(SQLException ex)
{
out.println("Unable to connect to database.");
}
%>
</body>
</html>
推荐答案
pl查看以下文章:
http://www.tutorialspoint.com/jsp/jsp_database_access.htm [ ^ ]
pl check the following article:
http://www.tutorialspoint.com/jsp/jsp_database_access.htm[^]
这篇关于使用jsp在mysql数据库中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文