
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

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