现在我在这里看到了一些问题,但我的问题有点不同。我创建了一个Create Account页面和Login页面。我在MYSQL中创建了一个数据库,其中存储了各种用户名和相应的密码。我使用创建帐户页面创建了一个帐户,然后通过“登录”页面进行了检查,发现它正在运行。但后来我尝试编写一个代码,在创建相应帐户后自动在MYSQL中创建表。但我得到了上述错误。我将向您显示我的登录和创建帐户页面。
LOGIN: -
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class Login extends Frame implements ActionListener, WindowListener
{
Label l1,l2,l3;
JTextField t1;
JPasswordField p1;
JButton b0,b1;
String url = "jdbc:mysql://localhost:3306/database1";
String user = "root";
String password = "Newyear2016!";
char[] s2;
String s1,s3;
Login()
{
addWindowListener(this);
setTitle("Login");
setSize(500,500);
setLayout(null);
setVisible(true);
l3 = new Label("Login");
l3.setBounds(120,50,300,50);
add(l3);
Font myFont1 = new Font("Helvetica",Font.ITALIC,30);
l3.setFont(myFont1);
l1 = new Label("User Name: ");
l1.setBounds(50,150,100,50);
add(l1);
l2 = new Label("Password: ");
l2.setBounds(50,250,100,50);
add(l2);
t1 = new JTextField();
p1 = new JPasswordField();
t1.setBounds(170,150,250,50);
p1.setBounds(170,250,250,50);
add(t1);
add(p1);
t1.addActionListener(this);
p1.addActionListener(this);
b0 = new JButton("Login");
b0.setBounds(170,350,100,50);
add(b0);
b0.addActionListener(this);
b1 = new JButton("Go Back");
b1.setBounds(300,350,100,50);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
s1 = t1.getText();
s2 = p1.getPassword();
s3 = new String(s2);
Object o = e.getSource();
if(o == b1)
{
dispose();
Home h = new Home();
}
mainLoop:
if(o == b0)
{
try
{
Connection myConn =
DriverManager.getConnection(url,user,password);
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from login");
while(myRs.next())
{
if(s1.equals(myRs.getString("usname"))&&s3.equals(myRs.getString("pwd")))
{
System.out.println("Username Found");
System.out.println("Password Found");
JOptionPane.showMessageDialog(null,"Login Successful:
"+s1 );
break mainLoop;
}
}
JOptionPane.showMessageDialog(null,"Wrong Username/Password");
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
public void windowClosing(WindowEvent e)
{
dispose();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
请告诉我我做错了什么。我已经检查过我提供了正确的密码。所以这不是问题。
创建帐户: -
import java.awt。*;
import java.awt.event。*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
导入javax.swing。*;
public class Login extends Frame实现了ActionListener,WindowListener
{
Label l1,l2,l3;
JTextField t1;
JPasswordField p1;
JButton b0,b1;
String url = "jdbc:mysql://localhost:3306/database1";
String user = "root";
String password = "Newyear2016!";
char[] s2;
String s1,s3;
Login()
{
addWindowListener(this);
setTitle("Login");
setSize(500,500);
setLayout(null);
setVisible(true);
l3 = new Label("Login");
l3.setBounds(120,50,300,50);
add(l3);
Font myFont1 = new Font("Helvetica",Font.ITALIC,30);
l3.setFont(myFont1);
l1 = new Label("User Name: ");
l1.setBounds(50,150,100,50);
add(l1);
l2 = new Label("Password: ");
l2.setBounds(50,250,100,50);
add(l2);
t1 = new JTextField();
p1 = new JPasswordField();
t1.setBounds(170,150,250,50);
p1.setBounds(170,250,250,50);
add(t1);
add(p1);
t1.addActionListener(this);
p1.addActionListener(this);
b0 = new JButton("Login");
b0.setBounds(170,350,100,50);
add(b0);
b0.addActionListener(this);
b1 = new JButton("Go Back");
b1.setBounds(300,350,100,50);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
s1 = t1.getText();
s2 = p1.getPassword();
s3 = new String(s2);
Object o = e.getSource();
if(o == b1)
{
dispose();
Home h = new Home();
}
mainLoop:
if(o == b0)
{
try
{
Connection myConn =
的DriverManager.getConnection(URL,用户名,密码);
Statement myStmt = myConn.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from login");
while(myRs.next())
{
如果(s1.equals(myRs.getString(" usname&#34))及;&安培; s3.equals(myRs.getString(" PWD")))
{
System.out.println("Username Found");
System.out.println("Password Found");
JOptionPane.showMessageDialog(null,"Login Successful:
" + s1);
break mainLoop;
}
}
JOptionPane.showMessageDialog(null,"Wrong Username/Password");
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
public void windowClosing(WindowEvent e)
{
dispose();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
} #Edit: - 对不起,我忘了提供创建帐户页面,这就是问题所在。能不能请你通过它?
答案 0 :(得分:0)
你的一些代码不在代码块中,所以很难读,但你加载了驱动程序类吗?
String url="jdbc:mysql://localhost:3306/database1";
String userName="root";
String password="Newyear2016!";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection(url,username,password);