mysql connector doesn't connect to my xxamp localhost - java

I am using netbeans 8.2 with xxamp. mysql connector.jar is 8.0.18.
It doesn't show any errors but whenever I press the button, the value from jUsername isn't inserted and instead the exception in the catch is shown. Please help.
private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String query = "INSERT INTO 'score' ('Name') VALUES (?)";
con = DriverManager.getConnection("jdbc:mysql://localhost/userscore", "root", "");
pst = con.prepareStatement(query);
pst.setString(1, jUsername.getText());
DifficultyPanel dpanel =new DifficultyPanel();
dpanel.setVisible(true);
this.dispose();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Please Enter Your Username To Continue");
}
}

Related

Issue when querying Ms-Access database Java

I am trying to make a login page where a user enters their username an password and it is checked in the database and whether or not the record is found the user would be granted access to the rest of my program. But for some reason there seems to be an issue with my database query and I cannot quite figure out why.
My code is below:
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
String userInput = txtUsername.getText();
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection("jdbc:ucanaccess://E:/Documents/School/IT/2020 PAT Database.accdb");
pst = con.prepareStatement("select * from tblLoginDetails WHERE Username = " + userInput);
rs = pst.executeQuery();
ResultSetMetaData rsd = rs.getMetaData();
String ID, username = "", password, isAdmin;
while(rs.next())
{
ID = rs.getString("ID");
username = rs.getString("Username");
password = rs.getString("Password");
isAdmin = rs.getString("IsAdmin");
if(username.equalsIgnoreCase(txtUsername.getText()))
JOptionPane.showMessageDialog(null, "Its working!");
}
}
//Exception handling for ClassNotFoundException and SQLException:
catch (ClassNotFoundException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Class Not Found\n"+e);
}
catch (SQLException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Invalid input\n"+e);
}
}
});
When i click on the login button I get an input error saying that the object is not found or user lacks privilege. Any help would be much appreciated.

How to set up online database for java netbeans application

i'm trying a online database connection for my java application, but it seems my database isn't connected well this is my java code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("sql101.epizy.com/epiz_26206530_sekolah","LOGIN","PASSWORD");
String query = "select * from users where username=? and password=?";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, jTextField1.getText());
pst.setString(2, jPasswordField1.getText());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
JOptionPane.showMessageDialog(null, "Login Berhasil");
jLabel7.setText(rs.getString(1));
welcome well = new welcome();
this.setVisible(false);
well.setVisible(true);
}
} catch (Exception e) {
}
}
i already input MySQL JDBC Driver library, by the way i'm using online database from https://infinityfree.net/

why delete button in jframe java error

I tried tutorial on youtube
I create the delete button in jframe java, then when I run an error occurs
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if (!jtextId.getText().equals(""))
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHRRE id = ?");
int id=Integer.parseInt(jtextId.getText());
ps.setInt(1, id);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Delete");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Produt Not Deleted");
}
}
else{
JOptionPane.showMessageDialog(null, "Enter Product");
}
}
On the output error is in ps.executeUpdate();
What should I fix??
If the code you pasted is your actual code, the error come from this line:
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHRRE id = ?");
You have a typo error in your SQL, replace by:
PreparedStatement ps = con.prepareStatement("DELETE FROM product WHERE id = ?");

Unable to insert data in mysql database using JDBC in Netbeans

I used it inside the button action made database, even connected the database but the information is not going inside the table of the data base.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhoast:3306//a","root","root");
Statement smt = conn.createStatement();
ps = conn.prepareStatement("insert into aone values (?,?,?)");
String n = name.getText();
String a = age.getText();
String r = roll.getText();
ps.setString(1,n);
ps.setString(2,a);
ps.setString(3,r);
int i = ps.executeUpdate();
if (i>0) {
JOptionPane.showMessageDialog(null, "data is saved");
}
else {
JOptionPane.showMessageDialog(null, "error");
}
}
catch(Exception e) {
}
}
Print the stack trace in the catch block. The JVM might be throwing an exception, but you'll never know it this way.
When you do that, I'm sure you'll be told that JDBC could not connect to "localhoast".
I doubt that "localhoast" is correct; try "localhost".
There are so many things wrong with this code:
Should not mingle UI and database code.
Should not get JDBC connection with every request; use a pool.
Code is not layered; hard to test.
You don't close any JDBC resources in method scope.
Try it like this:
private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a","root","root");
ps = conn.prepareStatement("insert into aone values (?,?,?)");
String n = name.getText();
String a = age.getText();
String r = roll.getText();
ps.setString(1,n);
ps.setString(2,a);
ps.setString(3,r);
int i = ps.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog(null, "data is saved");
} else {
JOptionPane.showMessageDialog(null, "error");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
close(ps); // You need to implement this
close(conn); // You need to implement this
}
}

How can I get the username after successful login?

I am trying to display username after user Login to the system in Java. There have any way?
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JDBC driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/unimas";
// Database credentials
final String USER = "root";
final String PASS = "shojib420";
Connection conn = null;
Statement stmt = null;
try {
dec();
} catch (NoSuchAlgorithmException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//String name=name2.getText();
String username=user1.getText();
//String faculty=fac1.getText();
if(username.equalsIgnoreCase("") || password.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(contentPane, "OOPS You miss some filed");
}
else
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
stmt = (Statement) conn.createStatement();
/* String sql="INSERT INTO useruser " +
"VALUES (username,password, name,faculty)";*/
String sql="SELECT * FROM useruser WHERE username='"+username+"' and password='"+password+"' ";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{
dashboard d=new dashboard();
d.setVisible(true);
d.name=username;
dispose();
}
else
{
JOptionPane.showMessageDialog(contentPane, "Sorry "+password);
}
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e1){
//Handle errors for Class.forName
e1.printStackTrace();
}
}
}
});
Here d.name=username; name is in the dashboard class where I am trying to set d.name=username; , But i am not able to get the value of username
JTextArea showName = new JTextArea();
showName.setBounds(29, 25, 105, 22);
contentPane.add(showName);
showName.setName(name);
I am calling that name in dashboard class. Any idea where I am doing wrong or better suggestion?
dashboard d=new dashboard();
d.setVisible(true);
d.name=username;
Here you just set class variable but the variable is not set in any label (or textarea or textfield).
Should be
dashboard d=new dashboard();
d.nameLabel.setText(username);
d.setVisible(true);

Categories