Wednesday, 11 December 2013

inserting the values connecting to the db using user id and password in Mysql

//connecting to the db using user id and password

import java.sql.*;
public class Ex2
{
 public static void main(String arg[]) throws SQLException, ClassNotFoundException
 {
  String driverClassName="com.mysql.jdbc.Driver";
  String url="jdbc:mysql://localhost:3306/test";
  String userName="root";
  String password="admin";
//  String q="insert into students values(101,\'kumar\')";//or
 // String q="insert into students values(102,'raj')";
 //load driver class,
  Class.forName(driverClassName);
 //get the connection
 Connection con=DriverManager.getConnection(url,userName,password);
 //get the statement
 Statement st=con.createStatement();
 //execute the query
// int count=st.executeUpdate(q);
//or
 int count=st.executeUpdate("insert into students values(103,'ram','sec')");
 System.out.println("no of rows "+count);
  //close the connection
 con.close();
 }//main
}//class
/*
D:\jdbc>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar;.;
D:\jdbc>javac Ex2.java
D:\jdbc>java Ex2
*/

No comments:

Post a Comment