gpt4 book ai didi

java - MySQL 连接到 java 应用程序

转载 作者:行者123 更新时间:2023-11-30 01:02:57 25 4
gpt4 key购买 nike

我已经在 phpadmin 中创建了一个名为 fbaccount 的表。我还是不知道这有什么问题。任何帮助将不胜感激。

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ImageIcon.*;
import java.sql.*;
public class Fb extends JFrame{

Icon icon = new ImageIcon("images/signup.png");
Icon icon2 = new ImageIcon("images/login.jpg");
JLabel signupicon = new JLabel(icon);
JLabel loginicon = new JLabel(icon2);
JTextField user = new JTextField();
JTextField fname =new JTextField("First name");
JTextField lname = new JTextField("Last name");
JTextField email = new JTextField("Email");
JTextField remail = new JTextField("Re-enter email");
JTextField password =new JTextField("Password");
JPasswordField pass = new JPasswordField();
Container c = new Container();
Connection con;
Statement st;
ResultSet rs;
int ctr=0;
public Fb()//constuctor
{

this.setSize(947,486);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setContentPane(new JLabel(new ImageIcon("images/fb.jpg")));
c = this.getContentPane();
c.setLayout(null);
c.add(signupicon);
c.add(loginicon);
c.add(user);
c.add(pass);
c.add(fname);
c.add(lname);
c.add(email);
c.add(remail);
c.add(password);
loginicon.setBounds(875,18,48,21);
signupicon.setBounds(526,405,195,40);
user.setBounds(547,18,151,21);
pass.setBounds(710,18,151,21);
fname.setBounds(524,179,195,40);
lname.setBounds(729,179,195,40);
email.setBounds(524,229,400,40);
remail.setBounds(524,279,400,40);
password.setBounds(524,329,400,40);


}
//for sql codes
public void actionPerformed(ActionEvent e)
{
//for login
Object j= e.getSource();
if(j==loginicon){
String u = user.getText();
String p = pass.getText();
try{
String driver="com.mysql.jdbc.Driver";
String db="jdbc:mysql://localhost:3306/fbaccount";
String user="root";
String pass="";
Class.forName(driver);
con=DriverManager.getConnection(db,user,pass);
st=con.createStatement();

String QueryString = "SELECT username, password from fbaccount WHERE username = '"+u+"' AND password = '"+p+"' ";
rs = st.executeQuery(QueryString);

int count = 0;
while(rs.next()){
count = count +1;
}
if (count==1)
{
Opening ms = new Opening();
ms.setVisible(true);
dispose();


}
else if (count>1){
JOptionPane.showMessageDialog(this,"Can't Login");


}

else if (count==0)
JOptionPane.showMessageDialog(this,"No record Exist");
}catch(Exception ex){
JOptionPane.showMessageDialog(this, "Error in database connection");
ex.printStackTrace();
System.exit(0);
}

}
//for the signup
else if (j==signupicon)
{
String u = user.getText();
String p = pass.getText();
String nemail=email.getText();
String n= fname.getText();
String l= lname.getText();

try{
int updateQuery = 0;
String driver="com.mysql.jdbc.Driver";
String db="jdbc:mysql://localhost:3306/fbaccount";
String user="root";
String pass="";
Class.forName(driver);
con=DriverManager.getConnection(db,user,pass);
st=con.createStatement();

String QueryString = "SELECT username, password from fbaccount WHERE username = '"+u+"' ";
rs = st.executeQuery(QueryString);

int count = 0;
while(rs.next()){
count = count +1;
}
if (count>0)
{
JOptionPane.showMessageDialog(this,"Username already Exist");
}
else{
QueryString = "INSERT INTO fbaccount(lastname, firstname,username ,new_email,password) VALUES ('"+l+"','"+n+"','"+u+"' ,'"+nemail+"','"+p+"')";

updateQuery = st.executeUpdate(QueryString);
{
JOptionPane.showMessageDialog(this,"Account created successfully");
}
}

}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "Unable to connect to database");
ex.printStackTrace();
System.exit(0);
}
}
}
public static void main(String[] args)
{

Fb f = new Fb();
f.setVisible(true);
}



}

我该怎么办?这是连接 mysql 的方法还是我遗漏了什么??

最佳答案

您还没有说明您的应用程序问题是什么,但让我指出代码中的几个明显问题。

  1. 您不仅会在每次用户操作时打开连接,而且每次都会加载 JDBC 驱动程序,但不建议这样做。您应该加载驱动程序并在应用程序启动时打开连接。

  2. 当您应该使用 PreparedStatement 时,您正在使用强力连接 SQL 语句以将数据插入数据库表中。它使您的代码容易受到SQL注入(inject)攻击,但是当字符串参数包含撇号等保留字符时,您应该首先担心不起作用的SQL语句。

    String sql = "INSERT INTO fbaccount(姓氏、名字、用户名、新电子邮件地址、密码) 值 (?,?,?,?,?)"; PreparedStatement ps = connection.prepareStatement(sql); ps.setString(1, l); ps.setString(2, n); ps.setString(3, u); ps.setString(4, nemail); ps.setString(5, p); ps.execute();

  3. 您的数据库也称为fbaccount,与数据库表相同吗? JDBC 连接 URL 应包含数据库名称,而不是数据库表的名称:

    jdbc:mysql://localhost:3306/fbaccount

关于java - MySQL 连接到 java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com