Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将HTML连接到java以在数据库中添加值_Java_Database_Jsp - Fatal编程技术网

如何将HTML连接到java以在数据库中添加值

如何将HTML连接到java以在数据库中添加值,java,database,jsp,Java,Database,Jsp,我想在从jsp文件输入的数据库中输入数据,但不知道如何连接它们。有人能建议我连接这两个文件并添加以jsp形式输入的数据吗 这是我的jsp和java文件 test1.java package P1; import java.sql.*; class test1 { public static void main(String[] args) throws SQLException, ClassNotFoundException {

我想在从jsp文件输入的数据库中输入数据,但不知道如何连接它们。有人能建议我连接这两个文件并添加以jsp形式输入的数据吗

这是我的jsp和java文件

test1.java

package P1;  

import java.sql.*;  


class test1 {  
        public static void main(String[] args) throws SQLException, ClassNotFoundException  {  


            Connection con = null;  

            try {  
                Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");  
            }  
            catch(ClassNotFoundException ex) {  
                System.out.println("Error: unable to load driver class!");  
                System.exit(1);  
            }  

            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");  
            Statement statement = con.createStatement();  

           String command = "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";  
           statement.executeUpdate(command);  

           con.close();  

       }  

} 
test1.html

package P1;  

import java.sql.*;  


class test1 {  
        public static void main(String[] args) throws SQLException, ClassNotFoundException  {  


            Connection con = null;  

            try {  
                Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");  
            }  
            catch(ClassNotFoundException ex) {  
                System.out.println("Error: unable to load driver class!");  
                System.exit(1);  
            }  

            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");  
            Statement statement = con.createStatement();  

           String command = "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";  
           statement.executeUpdate(command);  

           con.close();  

       }  

} 
**


形式
函数getvalues()
{
var name=document.getElementById(“名称”);
var roll=document.getElementById(“rollno”);
var clas=document.getElementById(“类”);
var mobile=document.getElementById(“mobileno”);
}
函数编号(e)
{
var-k;
document.all?k=e.keyCode:k=e.which;
返回(!((k>64&&k<91)| |(k>96&&k<123)| | k==8));
}
名字:
滚动:
类别:
流动电话:

**

我不会按你的密码行事。但这里的例子就足够了。 在纯
Servlets/JSP
world中,向服务器传递/提交数据的标准方法是使用
HTML
表单,即与使用其他服务器端语言(例如php)时的方法相同。不管它是纯HTML页面还是JSP页面。建议/最常用的从表单向服务器提交数据的方法是
POST
GET
。 它的标准方式是使用POST方法提交数据,并分别使用servlet中的
doPost()
方法处理提交的数据。 例如:

<form name="something" method="post" action="<servlet-name>"> //if u want to change the action to something else then u need to modify your xml file.
<input type="text" name="username"/>
<input type="submit" name="submitit" value="submited"/>
</form>
最后,您可以使用另一个页面重定向它

`response.sendRedirect();`
或者其他任何方式 我可以假设您正在使用EclipseJavaEEIDE进行开发。这样,您就不必担心如何集成它们,一旦您创建了一个新的JavaEE项目,eclipse就会为您准备xml文件。如果不是这样的话,你就必须手动操作,我曾经尝试过,但没有成功。 这里有一个链接:我希望,这会让你感到高兴:

这是件坏事,但我会为你编辑代码。顺便说一下,我正在删除javascript。亲吻(保持简单愚蠢)…) 您的jsp页面将是:

<%@ 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">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>FORM</title>

    </head>
    <body>
        <form action="test1" method="post" >
            <table>
                <tr>
                    <td>First Name: </td>
                    <td><input type="text" name="name" maxlength="10"></td>
                </tr>
                <tr>
                    <td>roll:</td>
                    <td><input type="text" name="rollno" maxlength="5"></td>
                </tr>
                <tr>
                    <td>class:</td>
                    <td><input type="text" name="class" maxlength="10"></td>
                </tr>
                <tr>
                    <td>Mobile:</td>
                    <td><input type="text" name="mobileno" maxlength="10"></td>
                </tr>       
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"></td>
                </tr>
            </table>

        </form>
    </body>
</html>

and the servlet will be:

import java.sql.*;  


class test1 {  
        public static void main(String[] args) throws SQLException, ClassNotFoundException  {  


            Connection con = null;
    PreparedStatement pstmt = null;
            try {  
                Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");  
            }  
            catch(ClassNotFoundException ex) {  
                System.out.println("Error: unable to load driver class!");  
                System.exit(1);  
            }  

            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");  

String name = request.getParameter("name");
        String roll = document.getElementById("rollno");// idk why roll no is string
        String clas_s = document.getElementById("class");
        String mobile = document.getElementById("mobileno");  
try {
           String query= "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";  
pstmt = con.prepareStatement(query);
pstmt.setString(1,name);
pstmt.setString(2,roll);
pstmt.setString(3,clas_s);
pstmt.setString(4,mobile);
           pstmt.executeUpdate();  

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

response.sendRedirect("confirm.jsp");

       } 
}

形式
名字:
滚动:
类别:
流动电话:
servlet将是:
导入java.sql.*;
类test1{
公共静态void main(字符串[]args)抛出SQLException,ClassNotFoundException{
连接con=null;
PreparedStatement pstmt=null;
试试{
forName类(“oracle.jdbc.odbc.JdbcOdbcDriver”);
}  
catch(ClassNotFoundException ex){
System.out.println(“错误:无法加载驱动程序类!”);
系统出口(1);
}  
con=DriverManager.getConnection(“jdbc:oracle:thin:@192.168.106.87:1521:ORA11G”、“fuel_db”、“foel”);
字符串名称=request.getParameter(“名称”);
String roll=document.getElementById(“rollno”);//idk为什么roll no是字符串
字符串clas_s=document.getElementById(“类”);
字符串mobile=document.getElementById(“mobileno”);
试一试{
String query=“插入学生(姓名、rollno、班级、mobileno)值(?、、?、?);”;
pstmt=con.preparest陈述(查询);
pstmt.setString(1,名称);
pstmt固定管柱(2,辊);
pstmt固定管柱(3类);
pstmt.设置管柱(4,移动式);
pstmt.executeUpdate();
con.close();
}
捕获(例外e){
e、 printStackTrace();}
sendRedirect(“confirm.jsp”);
} 
}

别问我牙套的事。。你自己修吧。

我不会按你的代码去做。但这里的例子就足够了。 在纯
Servlets/JSP
world中,向服务器传递/提交数据的标准方法是使用
HTML
表单,即与使用其他服务器端语言(例如php)时的方法相同。不管它是纯HTML页面还是JSP页面。建议/最常用的从表单向服务器提交数据的方法是
POST
GET
。 它的标准方式是使用POST方法提交数据,并分别使用servlet中的
doPost()
方法处理提交的数据。 例如:

<form name="something" method="post" action="<servlet-name>"> //if u want to change the action to something else then u need to modify your xml file.
<input type="text" name="username"/>
<input type="submit" name="submitit" value="submited"/>
</form>
最后,您可以使用另一个页面重定向它

`response.sendRedirect();`
或者其他任何方式 我可以假设您正在使用EclipseJavaEEIDE进行开发。这样,您就不必担心如何集成它们,一旦您创建了一个新的JavaEE项目,eclipse就会为您准备xml文件。如果不是这样的话,你就必须手动操作,我曾经尝试过,但没有成功。 这里有一个链接:我希望,这会让你感到高兴:

这是件坏事,但我会为你编辑代码。顺便说一下,我正在删除javascript。亲吻(保持简单愚蠢)…) 您的jsp页面将是:

<%@ 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">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>FORM</title>

    </head>
    <body>
        <form action="test1" method="post" >
            <table>
                <tr>
                    <td>First Name: </td>
                    <td><input type="text" name="name" maxlength="10"></td>
                </tr>
                <tr>
                    <td>roll:</td>
                    <td><input type="text" name="rollno" maxlength="5"></td>
                </tr>
                <tr>
                    <td>class:</td>
                    <td><input type="text" name="class" maxlength="10"></td>
                </tr>
                <tr>
                    <td>Mobile:</td>
                    <td><input type="text" name="mobileno" maxlength="10"></td>
                </tr>       
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"></td>
                </tr>
            </table>

        </form>
    </body>
</html>

and the servlet will be:

import java.sql.*;  


class test1 {  
        public static void main(String[] args) throws SQLException, ClassNotFoundException  {  


            Connection con = null;
    PreparedStatement pstmt = null;
            try {  
                Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");  
            }  
            catch(ClassNotFoundException ex) {  
                System.out.println("Error: unable to load driver class!");  
                System.exit(1);  
            }  

            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.106.87:1521:ORA11G","fuel_db","foel");  

String name = request.getParameter("name");
        String roll = document.getElementById("rollno");// idk why roll no is string
        String clas_s = document.getElementById("class");
        String mobile = document.getElementById("mobileno");  
try {
           String query= "INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ?);";  
pstmt = con.prepareStatement(query);
pstmt.setString(1,name);
pstmt.setString(2,roll);
pstmt.setString(3,clas_s);
pstmt.setString(4,mobile);
           pstmt.executeUpdate();  

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

response.sendRedirect("confirm.jsp");

       } 
}

形式
名字:
滚动:
<%
String name = request.getParameter("name");
//add null checks and all
//Similarly get all datamobileno etc
//then call your submitData() method
test1 myTest = new test1();
myTest.submitData(....)
%>