将代码写入数据库html的问题

时间:2015-04-16 18:40:03

标签: java html

我试图仅使用jsp页面将信息写入数据库,但我一直遇到此错误;

信息存储不成功java.sql.SQLException:必须在'之后指定端口:'在连接字符串

这是我的代码;



<%@page import="java.sql.*" %>
<%@page import="java.util.Date" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Player Register</title>
        <link rel='stylesheet'  href= 'styles.css'>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h2 align="center"> Register</h2>
        <form class="my_form" action="playerDisplay.jsp" method="POST">
            <fieldset>
                <legend>Please Enter Your Details</legend>
                <h1 class="h1a">New Player</h1>
                <p>
                    <label class="labela" for="login">Name:</label>
                    <input type="text" name="Name" id="Name" value="Name"required>
                </p>
                <p>
                    <label class="labela" for="login">Surname:</label>
                    <input type="text" name="Surname" id="Surname"                                                   value="Surname"required>
                </p>
                <p>
                    <label class="labela" for="login">PPS Number:</label>
                    <input type="text" name="PPS_Number" id="PPS_Number"                                            value="PPS_Number"required>
                </p>
                <p>
                    <label class="labela" for="login">Grade:</label>
                    <input type="text" name="Grade" id="Grade" value="Grade"required>
                </p>
                <p>
                    <label class="labela" for="login">Email:</label>
                    <input type="text" name="Email" id="Email" value="Email"required>
                </p>
                <p>
                    <label class="labela" for="password">Password:</label>
                    <input type="password" name="password" id="Password"                                             value="123456"required>
                </p>
                <p>
                    <input type="checkbox" name="mailingList" value="yes">Yes, add me to                             your mailing list<br>
                </p>
                <div style="text-align: center">
                    <input  type="submit" name="submit">
                    <input  type="reset" name="reset">
                </div>
            </fieldset>
        </form>                
    </body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <link href="CSS/styles.css" rel="stylesheet" type="text/css"/>
        <script src="js/libs/jquery/jquery.js"></script>
        <style>
            table, td, th {
            border: 1px solid black;
                }

            td {
                padding: 15px;
            }
            #optionalFieldset {
                display: none;
            }
        </style>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Your Space</title>
        <link rel='stylesheet'  href= 'styles.css'>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            $(document).ready(function () {
                $("#optionalCheck").click(function () { 
                    $("#optionalFieldset").slideToggle("slow");
                });
            });
        </script>
    </head>
    <body>
        <fieldset>
            <legend>Your Info</legend>
            <%
                String Name=request.getParameter("Name");
                String Surname=request.getParameter("Surname");
                String PPS_Number=request.getParameter("PPS_Number");
                String Grade=request.getParameter("Grade");
                String Email=request.getParameter("Email");
                String Password=request.getParameter("Password");
                        
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    Connection conn=DriverManager.getConnection("jdbc:mysql://http://danu6.it.nuigalway.ie:3306/mydb1899/players", "mydb1899a", "mydb1899a");
                    PreparedStatement ps = conn.prepareStatement("INSERT INTO players(Name,Surname,PPS_Number,Grade,Email,Password) VALUES(?,?,?,?,?,?)");
                    ps.setString(1,Name);
                    ps.setString(2,Surname);
                    ps.setString(3,PPS_Number);
                    ps.setString(4,Grade);
                    ps.setString(5,Email);
                    ps.setString(6,Password);
                    ps.executeUpdate();
                    out.println("Your information was successfully stored in our database");
                    conn.close();
                    ps.close();
                } 
                catch(Exception e) {
                    out.println("information storage was unsuccessful " + e);
                }
            %>
                
        </fieldset>   
        <a href="index.html">Home</a>                  
    </body>
</html>
&#13;
&#13;
&#13;

我有什么方法可以这样做,如果有,我有错误的解决方案吗?

2 个答案:

答案 0 :(得分:0)

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

这是连接URL的格式。你的工作吗?

答案 1 :(得分:0)

我认为您不希望http://在那里,对于正确的URL,您可能需要MySQL:// - 或 - http://不是两者。

我猜你正确的字符串将是:

JDBC:MySQL的://danu6.it.nuigalway.ie:3306 / mydb1899 /播放器