當前位置:編程學習大全網 - 編程語言 - [jsp例題]為什麽不能插入數據呢..哭了...

[jsp例題]為什麽不能插入數據呢..哭了...

分類: 電腦/網絡 >> 程序設計 >> 其他編程語言

問題描述:

<>

<head>

<title>信息查詢系統</title>

</head>

<%@page contentType="text/; charset=GB2312"%>

<body>

<h1 align=center class="style1">信息查詢系統 </h1>

<form name=form1 method="post" action="insert.jsp">

<p>

姓名:

<input type=text name=names>

電話:

<input type=text name=phone>

<input type=submit name=Submit value="插入">

</p>

</form>

<p align=left class="style1"> </p>

</body>

</>

---------------------- insert.jsp ---------------------

<%@ page import="java.sql.*" %>

<%@ page contentType="text/;charset=GB2312" %>

<%

String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

String url = "jdbc:odbc:wish";

String name = request.getParameter("names");

String phone = request.getParameter("phone");

out.println(name+"<br>"); 值已經取到了

out.println(phone+"<br>");

就是不能插入數據庫..暈.為什麽?

String insert = "INSERT INTO worker"

+"(names,phone)"

+"VALUES('name','phone')";

try

{

Class.forName(driver);

}

catch(Exception e)

{

e.printStackTrace();

}

try

{

Connection con = DriverManager.getConnection(url);

out.print("連接成功。");

Statement *** t = con.createStatement();

*** t.executeUpdate(insert);

*** t.close();

con.close();

}

catch(Exception e)

{

e.printStackTrace();

out.print(insert); 打印出來的拿到access裏面查詢運行也能正確插入...

}

%>

為什麽不能插入數據呢..哭了...

怎麽也插入不進去.也沒有錯誤提示...把sql語句打印,拿到access裏面查詢裏面運行也可以..在這就是不行..

odbc數據源起名什麽的沒有問題.我單獨插入能插,加上壹個,在插就插不進去了.估計是sql那裏有寫錯的地方.

麻煩大家不忙時候看看....著急啊......呵呵

解析:

對,妳的sql寫錯了。

String insert = "INSERT INTO worker"

+"(names,phone)"

+"VALUES('name','phone')";

的結果是

INSERT INTO worker (names,phone)

VALUES('name','phone')

看出問題了嗎?

妳需要的是將變量name和phone的值存入數據庫而不是字符串"name"和"phone"

正確的寫法是:

String insert = "INSERT INTO worker"

+"(names,phone)"

+"VALUES('" + name " + "','" + phone + "')";

  • 上一篇:孩子還小,有必要學編程嗎?
  • 下一篇:梯形圖設計?
  • copyright 2024編程學習大全網