Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


tejasri
java
1 Answer(s)      3 years and 10 months ago
Posted in : SQL


import java.sql.*; import java.io.*; class MysqlSample { public static void main(String[] args) { try {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter student number");
        String tno=br.readLine();
        int no=Integer.parseInt(tno.trim());

        System.out.println("Student name");
        String name=br.readLine();

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("driver class is loaded");

        Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/mysql","root","root");
        System.out.println("connection is established");

        Statement st= conn.createStatement();
        System.out.println("Statement object is created");

        String query="insert into student1 values("+no+",' "+name+" ')";
        System.out.println(query);
        System.out.println("Print the Query...");

        ResultSet rs=st.executeQuery(query);
        System.out.println("Query is executed...");

    /*  if(rs==0)
            System.out.println("record is not inserted");
        else
            System.out.println("record is inserted");*/
    }
    catch (SQLException se)
    {
        se.printStackTrace();
    }       
    catch (Exception e)
    {
        e.printStackTrace();
    }       
}

} please help me where is the wrong statement is

regards teja


Advertisement
View Answers

October 20, 2010 at 11:30 AM


Hi Friend,

Yo have used wrong method to insert the data. The method executeUpdate() is used to insert the data. Moreover, you haven't defined the fields whose values you are going to insert. Anyways, we have modified your code:

import java.sql.*;
import java.io.*; 
class MysqlSample {
    public static void main(String[] args) {
        try {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter student number");
        String tno=br.readLine();
        int no=Integer.parseInt(tno.trim());

        System.out.println("Student name");
        String name=br.readLine();

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("driver class is loaded");

        Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/mysql","root","root");
        System.out.println("connection is established");

        Statement st= conn.createStatement();
        System.out.println("Statement object is created");

        String query="insert into student1(rollNo,name) values("+no+",' "+name+" ')";
        System.out.println(query);
        System.out.println("Print the Query...");
        int i=st.executeUpdate(query);
        System.out.println("Query is executed...");
    }
    catch (SQLException se)
    {
        se.printStackTrace();
    }       
    catch (Exception e)
    {
        e.printStackTrace();
    }       
}
}

Thanks



Related Tutorials/Questions & Answers:
java
java   What is ?static? keyword
java
java  hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
Advertisements
java
java  1.what type of inheritance supported in java. 2.what is the advantage of java over C++.   Hi Friend, Java supports following... inheritance Advantages of Java over C++: 1)Java is pure object oriented
Java
Java   What is the immediate superclass of the Dialog class
java
java  i need to draw a binary tree traversal in java applet
java
of java connection to the database to the java program   Hello Friend, Please visit the following link: Java Database Connectivity Here you will find several examples related to java database connectivity. Thanks
java
java  why we pass reference as a parameter in any java method
java
java  how to learn d java in easy way   Hi Friend, Please visit the following link: Java Tutorials Thanks
java
java  please any one give me idea for how to learn applet in java and then any one tell me concept for applet in java
JAVA
JAVA  Name Explanation about Java....   Hi Friend, Please clarify your problem. Thanks   Abbreviation of JAVA
JAVA
JAVA  If anybody know the abbreviation of JAVA....   JAVA is a full functioning programming structure by Sun Microsystems there is no abbreviation, and it is not the abbreviation (or acronym) of anything
java
, visit the following links: http://www.roseindia.net/java/master-java/java-left-shift.shtml http://www.roseindia.net/java/master-java/java-right-shift.shtml
java
java  can we implement two main class in java?   Hi Friend, A java class should have only one main method. Thanks
Java
Java  How to install java on linux ubuntu?   Hi Friend, Please visit the following links: http://www.roseindia.net/linux/linux-java-install.shtml http://www.roseindia.net/linux/tutorial/installingjdk5onlinux.shtml
java
java  write a programe in java such as child thread excutes 3 times more than main thread
JAva
JAva   how to make a class final without using final keyword
java
java  write a programe in java such as child thread excutes 3 times more than main thread
java
java  where i write java programme and how do i execute the programme
Java
Java  java program to get output on the basis of users choice of switch statement perform operations addition, subtraction and multiplication of matrix
java
java  hi all i have a problem ,i want to sotre form data in table with foreign key in java ,please help me  Hi,Are you developing desktop or JSP application?Thanks
java
java  What is the purpose of Void class?  The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void
java
java  I need to draw a Binary tree using Inorder traversal algorithm.. in java applets using rectangles and ellipse objects.. can anyone help me about
java
inheritance. Java does not support multiple inheritance but the multiple inheritance...://www.roseindia.net/java/language/inheritance.shtml
java
java  Pls help me with project or pls give me guide lines to this: Hi friends and how are you? i have a problem i need to write a java program which has a two java packages, one has the interface and the other has classes
java
java  Hi friends and how are you? i have a problem i need to write a java program which has a two java packages, one has the interface and the other has classes, with each class calculating the conversion of the South african
java
java  What is DriverManager?  The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. Usually Driver Manager is the backbone of the JDBC architecture
java
java  <p>java program to find short form of namefor eg . sudhir kumar tiwari output should be s.k tiwari</p>  Hi Friend, import java.util.*; public class InitialName { public static void main
Java
Java  Hi I need help to edit values in database into jsp page textboxes, This is my code: editdata.jsp <%@ page language="java" import="java.util.*;"%> <%@ page language="java" import="java.sql.*"%> <
Java Fundamentals - Java Beginners
Java Fundamentals  What is the difference between Java Bean and Java Class
java - Java Interview Questions
Java Programming  What is Java Programming language
java
/TR/html4/loose.dtd"> <%@ page language="java" import="java.sql.*"%> <title>
Java - Java Interview Questions
Java  Questions on Java
java+cts - Java Beginners
java+cts  4100156952
core java - Java Beginners
core java  i want to get java projects in core java
java - Java Magazine
Java string array declaration  How to declare a string array in Java
Java - Java Beginners
Java enum class tostring  An example of Java enum class tostring
java on linux - Java Beginners
java on linux  How to compile and execute java applications in linux
Java Questions - Java Beginners
Java Questions  Can we make 3d games in java
Java tools - Java Beginners
Java tools  how can we create a tool using java
java threads - Java Beginners
java threads  What is Thread in Java and why it is used
Java Gui - Java Beginners
Java Gui  HOW TO ADD LINK LABELS IN JAVA PROGRAMMES
Java GUI - Java Beginners
Java GUI  HOW TO ADD ICONS IN JAVA GUI PROGRAMMES
JAva Projects - Java Magazine
JAva Projects  I need Some Java Projects
Java - Java Beginners
Java   HOW TO MAKE 3D GAMES IN JAVA
java - Java Beginners
Java GUI Builder software  Java GUI Builder software
java - Java Beginners
java  what are the logics of java
java - Java Interview Questions
java  how to learn java
java - Java Beginners
Java virtual machine  What is java virtual machine?  Hello,Please check http://www.roseindia.net/help/java/f/java-foreach.shtmlThanks
java - Java Beginners
Java keywords with examples  Java keywords with example to learn Java programming fast
java - Java Beginners
Java coding standards for developors  What is the java coding standards for Java application developers
 

 

 

DMCA.com