如何使用java中的setter在数据库中存储数据

时间:2015-10-26 05:00:08

标签: java mysql

我正在开发简单的程序,我必须接受用户的个人数据并将其存储在DB中。在主类我接受一个人的参数,如姓名,年龄等,并将该参数发送到SimpleExample类的setter方法,它将检查其有效性,如果数据有效,那么我想将其存储在DB中。

这是我的主要班级。

public class Football {

    public static void main(String[] args) throws SQLException, Exception {

        Scanner in= new Scanner(System.in);
        String name,education;
        int age;

            SimpleExample s=new SimpleExample();
        System.out.println("Enter your name:");
        name=in.next();
        s.setName(name);
        System.out.println("Enter you Age");
        age=in.nextInt();
        s.setAge(age);
        System.out.println("Enter your Education");
        education=in.next();
        s.setEducation(education);
        s.insert();     

    }

这是我的SimpleExample类。

   public class SimpleExample {


    private String name,education;
    private int age;


    public void setName(String name) {
        this.name = name;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public void setAge(int age) {
        this.age = age;
    }



    public void insert() throws SQLException,Exception{
        Connection con;
        Statement stmt;

            Class.forName("com.mysql.jdbc.Driver");  
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/person","root","Welcome123"); 
            stmt=con.createStatement();

            String sql="INSERT INTO personalInfo (name,age,education) VALUES ('"+name+"','"+age+"','"+education+"')";
            stmt.executeUpdate(sql);
    }
}

但是它给了我错误ClassNotFoundException。

  

线程中的异常" main" java.lang.ClassNotFoundException:java.net.URLClassLoader中的com.mysql.jdbc.Driver $ java。java.ProLC上的$ 1.run(未知来源)$ 1.run(java.security.AccessController.doPrivileged中的未知来源)(本机方法) )java.lang.ClassLoader.findClass(Unknown Source)at java.lang.ClassLoader.loadClass(Unknown Source)at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source)at java.lang.ClassLoader.loadClass(Unknown Source) )在java.lang.Class.forName0(本机方法) -

2 个答案:

答案 0 :(得分:1)

很可能是因为mysql JDBC驱动程序jar不存在或未添加到类路径而导致错误。

要从Java连接到MySQL,您必须使用MySQL的JDBC驱动程序。 MySQL JDBC驱动程序称为MySQL Connector / J.您可以在以下URL找到最新的MySQL JDBC驱动程序:http://dev.mysql.com/downloads/connector/j

然后,您需要将JDBC驱动程序添加到类路径中。

由于不清楚您是否使用IDE,因此请不要使用相同的说明。

完成上述步骤后,您的程序应该可以运行。如果没有尝试下面的程序来检查mysql连接是否正确。

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample {

  public static void main(String[] argv) {

    System.out.println("-------- MySQL JDBC Connection Testing ------------");

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager
        .getConnection("jdbc:mysql://localhost:3306/person","root", "Welcome123");

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
  }
}

我已经测试了您的代码并且它正在运行。 所以要么你正在修改一些import语句或JDBC jar文件,要么你的jar文件不在你的类路径中

尝试将jar文件放在java安装的ext文件夹中。在java \ jre.xx \ lib \ ext文件夹中。如果它在那之后工作那么那就是类路径问题。修复它。

使用import语句等代码粘贴代码(已成功运行的已测试代码)

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class SimpleExample {


    private String name,education;
    private int age;


    public void setName(String name) {
        this.name = name;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public void setAge(int age) {
        this.age = age;
    }



    public void insert() throws SQLException,Exception{
        Connection con;
        Statement stmt;

            Class.forName("com.mysql.jdbc.Driver");  
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/person","root","Welcome123"); 
            stmt=con.createStatement();

            String sql="INSERT INTO personalInfo (name,age,education) VALUES ('"+name+"','"+age+"','"+education+"')";
            stmt.executeUpdate(sql);
    }
}

Football.java

import java.sql.SQLException;
    import java.util.Scanner;

public class Football {

    public static void main(String[] args) throws SQLException, Exception {

        Scanner in= new Scanner(System.in);
        String name,education;
        int age;

            SimpleExample s=new SimpleExample();
        System.out.println("Enter your name:");
        name=in.next();
        s.setName(name);
        System.out.println("Enter you Age");
        age=in.nextInt();
        s.setAge(age);
        System.out.println("Enter your Education");
        education=in.next();
        s.setEducation(education);
        s.insert();     

    }

} 

有关详细信息,请查看以下链接

http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

http://www.vogella.com/tutorials/MySQLJava/article.html

答案 1 :(得分:0)

添加mysql JDBC驱动程序jar不存在或未添加到类路径

package in.india.core;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Football {

    public static void main(String[] args) throws SQLException, Exception {

        Scanner in = new Scanner(System.in);
        String name, education;
        int age;

        SimpleExample s = new SimpleExample();
        System.out.println("Enter your name:");
        name = in.next();
        s.setName(name);
        System.out.println("Enter you Age");
        age = in.nextInt();
        s.setAge(age);
        System.out.println("Enter your Education");
        education = in.next();
        s.setEducation(education);
        s.insert(s.getName(), s.getAge(), s.getEducation());

    }

}

class SimpleExample {

    private String name, education;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void insert(String name, int age, String education)
            throws SQLException, Exception {
        Connection con;
        Statement stmt;

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/person",
                "root", "Welcome123");
        stmt = con.createStatement();

        String sql = "INSERT INTO personalInfo (name,age,education) VALUES ('"
                + name + "','" + age + "','" + education + "')";
        stmt.executeUpdate(sql);
    }

}