#java code for connection with jdbc
import java.sql.*; public class abc {
public static void main(String args[])throws Exception{
# my driver which i am using
String url = "jdbc:mysql://localhost:3306//kushagra";
String user="root@localhost";
String password="kushagra";
#query written in popsql this a mysql query
String query = "Select student_name From students where student_id =1";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
rs.next();
String name = rs.getString("student_name");
System.out.println(name);
st.close();
con.close();
}
}
CodePudding user response:
user chanage to 'root'
String user="root";
CodePudding user response:
First of all, please confirm whether you have correctly imported the java connection database driver package in VS Code. You can click the plus sign after Referenced Libraries in JAVA PROJECTS to import.
In addition, String user="root@localhost"
in the code can be modified to String user="root"
.