I am first time trying to update events from DB in fullcalendar(v2.0.0).Here is my code i tried,
$('#calendar').fullCalendar({
editable: true,
events:"/FullcalendarProject/FullCalendarChecking",
eventDrop : function(event,revertFunc){
//here my $ajax(){ update DB }
}
});
FullCalendarChecking.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List l = new ArrayList();
PreparedStatement st = null;
ResultSet rst = null;
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5","root","");
String query="select * from tblplanner where User_id=?";
st = con.prepareStatement(query);
st.setString(1, "112");
rst =st.executeQuery();
String prepand="{title: ";
String st1="start: ";
String postpand="}";
while(rst.next())
{
System.out.println("entered...");
String plannedDate =rst.getString("date"); // this will be start in fullcalendar
String plannedChapter =rst.getString("chapter"); // this will be title in fullcalendar
System.out.println(plannedDate);
String afterchange =prepand+"'"+plannedChapter+"'"+","+st1+"'"+plannedDate+"'"+postpand;
l.add(afterchange);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{
con.close();
rst.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println(l);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(new Gson().toJson(l));
}
Output JSON:
["{title: \u0027Plants\u0027,start: \u00272015-04-28\u0027}","{title: \u0027Plants\u0027,start: \u00272015-05-10\u0027}","{title: \u0027Plants\u0027,start: \u00272015-05-12\u0027}","{title: \u0027Plants\u0027,start: \u00272015-05-13\u0027}","{title: \u0027Plants\u0027,start: \u00272015-05-08\u0027}","{title: \u0027Animals\u0027,start: \u00272015-05-12\u0027}"]
why all the events are displayed in today's date and showing current time instead of title ?
where am i doing it wrong ?
Related
In my SQL database single record consists of four rows: id, name, age and email. How to get a single record by typing in a JTextField id of that record? So later we can for example System.out.printIn(); it? I know that my question might be stupid for someone who is an SQL expert but I am only a beginner and after searching for this information in the tutorials I could not find it:(. Please help. Here is some of my source code:
public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://some IP address/testdb";
String username = "some username";
String password = "some password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch(Exception e){System.out.println(e);}
return null;
}
public EsquelTest() {
IDname = new JTextField("");
submit = new JButton("go");
add(IDname);
add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit) {
id = IDname.getText().toString();
try {
getConnection();
get(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
setLayout(new GridLayout());
}
public static ArrayList<String> get(String idname) throws Exception{
try{
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement("Statement needed to get the whole record by owning only an ID");
ResultSet result = statement.executeQuery();
ArrayList<String> array = new ArrayList<String>();
while(result.next()){
array.add(result.getString("last"));
}
System.out.println("All records have been selected!");
return array;
}catch(Exception e){System.out.println(e);}
return null;
}
If you are asking for only SQL statement it is: select * from yourtable where id = theIdThatIsfromTextFieldHere
Yet, if you simply google it you will find thousands of answers. here for instance.
The SQL Statement would be SELECT * FROM yourtable WHERE id = yourid. So to embed it into your code it would look something like this:
public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://some IP address/testdb";
String username = "some username";
String password = "some password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch(Exception e){System.out.println(e);}
return null;
}
public EsquelTest() {
IDname = new JTextField("");
submit = new JButton("go");
add(IDname);
add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit) {
id = IDname.getText().toString();
try {
getConnection();
get(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
setLayout(new GridLayout());
}
public static ArrayList<String> get(String idname) throws Exception{
try{
Connection con = getConnection();
PreparedStatement statement = con.prepareStatement("SELECT * FROM {REPLACE WITH YOUR TABLE} WHERE id = "+idname);
ResultSet result = statement.executeQuery();
ArrayList<String> array = new ArrayList<String>();
while(result.next()){
array.add(result.getString("last"));
}
System.out.println("All records have been selected!");
return array;
}catch(Exception e){System.out.println(e);}
return null;
}
Just a Tip: Don't name you function "get" as this is a commonly used keyword in other programming languages that only causes confusion.
Try it:
public static Connection getConnection() throws Exception{
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://some IP address/testdb";
String username = "some username";
String password = "some password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return conn;
} catch(Exception e){System.out.println(e);}
return null;
}
public EsquelTest() {
IDname = new JTextField("");
submit = new JButton("go");
add(IDname);
add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit) {
id = IDname.getText().toString();
try {
getConnection();
for(String string:get(id)){
System.out.println(string);
}
// get(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
setLayout(new GridLayout());
}
public static ArrayList<String> get(String idname) throws Exception{
try{
Connection con = getConnection();
// You should replace "YourTableName" With table in databace that you work with it
PreparedStatement statement = con.prepareStatement("SELECT * FROM YourTableName WHERE id = '" + idName+"'");
//If type of your id in database is Int write this code :
// int id= Integer.parseInt(idName);
//PreparedStatement statement = con.prepareStatement("SELECT * FROM YourTableName WHERE id = " + idName);
ResultSet result = statement.executeQuery();
ArrayList<String> array = new ArrayList<String>();
result.next();
//This array has all data in single recorde
array.add(result.getString("id"));
array.add(result.getString("name"));
array.add(result.getString("age"));
array.add(result.getString("email"));
// I removed this rows becuse you have only one record
// while(result.next()){
//
// array.add(result.getString("last"));
// }
System.out.println("All records have been selected!");
return array;
}catch(Exception e){System.out.println(e);}
return null;
}
I have exactly same function in both Main method and other method JDBC connection is working fine. If I call the other function it throws error java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/wine:
I have included MySql Driver in library [Netbeans];
processRequest method:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String process = (String) request.getParameter("process");
String name = (String) request.getParameter("process");
String phone=(String) request.getParameter("phone");
String email = (String) request.getParameter("email");
String pwd = (String) request.getParameter("pwd");
PrintWriter out = response.getWriter();
out.println("Hello");
signup(out,process,name,email,phone,pwd);
}
signup method:
private static int signup(PrintWriter out,String process,String name,String email,String phone,String pwd){
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");
out.println("Process Not Found");
if (process == "signup") {
String query = "INSERT INTO user(name,phone,email,password,role) VALUES(?,?,?,?,1)";
stmt = con.prepareStatement(query);
stmt.setString(1, name);
stmt.setString(2, phone);
stmt.setString(3, email);
stmt.setString(4, pwd);
stmt.execute();
} else {
out.println("Process Not Found");
}
} catch (SQLException e) {
// do something appropriate with the exception, *at least*:
out.println(e);
e.printStackTrace();
return 0;
}
return 1;
}
Main Method :
public static void main(String[] args) {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String process = "signup";
String name = "Test";
String phone="45885";
String email = "Test#gmail.com";
String pwd = "dkjsdh";
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wine", "root", "");
if (process == "signup") {
String query = "INSERT INTO user(name,phone,email,password,role,status) VALUES(?,?,?,?,1,1)";
stmt = con.prepareStatement(query);
stmt.setString(1, name);
stmt.setString(2, phone);
stmt.setString(3, email);
stmt.setString(4, pwd);
stmt.execute();
} else {
System.out.println("Process Not Found");
}
} catch (SQLException e) {
// do something appropriate with the exception, *at least*:
System.out.println(e);
e.printStackTrace();
}
}
There are two options that can be tried:
Class.forName("com.mysql.jdbc.Driver").newInstance() //older bug
and
Class.forName("com.mysql.jdbc.Driver");
In the comment I pasted the older bug solution when I meant to paste the second one.
Either way I am glad that it worked out for you
I have inputted the date through html page and trying to Store it to database but i got this exception java.lang.NumberFormatException: For input string: "09/12/2016" and i am also getting an error in netbeans "to surround Date date = (Date)formatter.parse(pswd); with try and catch" here is my code:
public class OfferRide extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String status = "";
try {
String userName = req.getParameter("Source");
String email = req.getParameter("Destination");
String pswd = req.getParameter("Date");
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
java.sql.Date date = (java.sql.Date)formatter.parse(pswd);
String id = req.getParameter("Seats");
int seat =Integer.parseInt(id);
String id1= req.getParameter("Phone");
long phone = Long.parseLong(id1);
Connection con = DataBaseServices.getConnection();
PreparedStatement ps = con.prepareStatement("insert into offerride values(?,?,?,?,?)");
ps.setString(1, userName);
ps.setDate(2, date);
ps.setString(3, email);
ps.setInt(4, seat);
ps.setLong(5, phone);
int x = ps.executeUpdate();
if (x == 1) {
req.getRequestDispatcher("OfferRide1.html").include(req, resp);
}
} catch (Exception e) {
status = e.toString();
//req.getRequestDispatcher("OfferRide1.html").include(req, resp);
}
PrintWriter out = resp.getWriter();
out.print(status);
}
}
place this code in between try and catch block
try
{
DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
Date date = (Date)formatter.parse(pswd);
}
catch(ParseException e)
{
}
i'm trying to use jdbc connection pool, for that i create IntialContext obj and datasource obj, and give "dsjndi" as parameter of lookup method of initialcontext, but when i run my project project it shows ""Name [dsjndi] is not bound in this Context. Unable to find [dsjndi].""
What is the problem can anyone guide me?
Connection con = null;
Statement st = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
public void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
//general settings
//get print Writer obj
PrintWriter pw = res.getWriter();
//set content type
res.setContentType("text/html");
//read form data
String tableName = req.getParameter("table");
try
{
//get con obj from jdbc con pool
con = makeConnection();
//create Statement obj
st = con.createStatement();
//execute query
rs = st.executeQuery("select * form" +tableName);
//use metadata obj to get db vlaues along with the col names
rsmd = rs.getMetaData();
int cnt = rsmd.getColumnCount();
pw.println("<hmtl><body bgcolor='#ffffa9'>");
pw.println("<center><h3>the details of the table" +tableName);
pw.println("<br><br><table cellsapcing='1' bgcolor='#00ff00'>");
pw.println("<tr bgcolor='#ffffa9'>");
//pritnt col names
for(int col=1;col<=cnt;col++)
{
pw.println("<th>" +rsmd.getColumnLabel(col)+ "  ");
}
pw.println("</tr>");
//print col values
while(rs.next())
{
pw.println("<tr bgcolor='#ffffa9'>");
for(int i=1;i<=cnt;i++)
{
pw.println("<td>" +rs.getString(i)+ " </td>");
}
pw.println("</td>");
}//while
pw.println("<table><br/>");
pw.println("<b>to view another table</b><ahref='index.html'>click here</a>");
pw.println("</body></html>");
}//try
catch(Exception e)
{
e.printStackTrace();
}
}
private Connection makeConnection()
{
Connection con = null;
try
{
//get IntialConetext
InitialContext ic = new InitialContext();
//get data sourse obj
DataSource ds = (DataSource) ic.lookup("DsJndi");
//get jdbc con obj from con pool
con = ds.getConnection();
}//try
catch(Exception e)
{
e.printStackTrace();
}
return con;
}//make Connection
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
process(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
process(req,res);
}
}
I am trying to make a simple login form. Every thing is working fine, connection is established, query is executed but ResultSet is still empty so always getting redirected to fail.jsp. No error no warning at all.
Servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
modelclass md = new modelclass();
daoclass dao = new daoclass();
md.setName(name);
md.setPassword(password);
System.out.println("this just before the sql query on the main servlet page");
String sql = "Select * from USERADD where name = ? and password= ?";
String result = dao.guser(md, sql);
if (result.equals("success")) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("fail.jsp");
}
}
This is the DAO class which makes connection.
Data Access code(dao.java):
public class daoclass {
public static String username = "NickNeo";
public static String password = "123123";
public static String driver = "com.ibm.db2.jcc.DB2Driver";
public static String url = "jdbc:db2://localhost:50000/CITYLIFE";
public static Connection con = null;
public static PreparedStatement ps = null;
static {
try {
Class.forName(driver);
System.out.println("before connection");
con = DriverManager.getConnection(url, username, password);
System.out.println("Connection Successfullll......!!!!!!");
} catch(Exception e) {
e.printStackTrace();
}
}
public String guser(modelclass obj, String sql) {
try {
System.out.println("entry into try block");
ps=con.prepareStatement(sql);
ps.setString(1, obj.getName());
ps.setString(2, obj.getPassword());
System.out.println("before query");
ResultSet rs = ps.executeQuery();
System.out.println("query executed");
int i = 0;
while(rs.next()) {
System.out.println("entered into while loop");
++i;
}
if (i >= 1) {
return "success";
} else {
System.out.println("this is inside else of while block");
return "fail";
}
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("this is the most outer fail statement");
return "fail";
}
}
the rs is always empty. tried many things but still getting rs as empty. please help