Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem inputing to MySQL table

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello, I am trying to input data into a mySQL database using a JSP but when I run it through localhost I am getting an error saying- root cause java.lang.NullPointerException.
Dose anyone know what the problem is. I have put my code that is causing the problem below. Its taking in the information from another page alright. It just wont write it to the table. Any feedback would be much appreciated.

<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.sql.*" %>
<%
Class.forName("org.gjt.mm.mysql.Driver");
String strUrl = "jdbc:mysql://localhost/niall";
Connection conn=DriverManager.getConnection (strUrl, "root", "password");
Statement stmt=conn.createStatement();
Connection con = null;
%>
<% // Fetch the form data
String into = request.getParameter("name");


// save info into the user's session
session.setAttribute("name", into);

//out.println(into); //It prints out here alright so it has takin in the information

String template = "INSERT INTO test (name) VALUE(?)";
PreparedStatement pstmt = con.prepareStatement(template);

pstmt.setString(1, into);
pstmt.executeUpdate();



if (pstmt!=null) pstmt.close();
if (con!=null) con.close();
%>

</li>
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Niall,
There are two objects: "conn" and "con" that you are using interchangeably. "Conn" has the value, so when you refer to "con" it is null.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Niall, please do not cross-post the same question in more than one forum. It wastes people's time when multiple redundant conversations take place.
[ May 04, 2005: Message edited by: Bear Bibeault ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic