How to make connection using jdbc,mysql,java source code
import java.sql.*;
import java.util.*;
public class FirstActivity
{
public static void main (String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println ("Enter the Name");
String name=scan.nextLine();
System.out.println ("Enter the Salary");
double salary=scan.nextDouble();
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/university?autoReconnect=true&useSSL=false","root","root");
if(!cn.isClosed())
{
System.out.println ("database server connected");
PreparedStatement ps=cn.prepareStatement("insert into employee(name,salary)values(?,?)");
ps.setString(1,name);
ps.setDouble(2,salary);
int a=ps.executeUpdate();
if(a>0)
{
System.out.println ("record inserted");
}
}
cn.close();
}
catch(Exception e)
{
System.out.println ("Error is :"+e.getMessage());
}
}
}
Additional Reading
Post Views: 526