JavaSpring框架
我想知道这段代码的错误是什么,我把一切都做好了,但为什么它不起作用呢?这是代码,我得到了我的驱动程序名和数据库url的权利,但为什么它不工作JavaSpring框架,java,sql,spring,jdbc,log4j,Java,Sql,Spring,Jdbc,Log4j,我想知道这段代码的错误是什么,我把一切都做好了,但为什么它不起作用呢?这是代码,我得到了我的驱动程序名和数据库url的权利,但为什么它不工作 package test; import java.util.List; import org.springframework.jdbc.datasource.DriverManagerDataSource; import dao.DerbyDao; import domainmodel.Person; public final class Mai
package test;
import java.util.List;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import dao.DerbyDao;
import domainmodel.Person;
public final class Main {
private Main() {
};
public static void main(String[] args) {
DerbyDao dao = new DerbyDao();
// Initialize the datasource, could /should be done of Spring
// configuration
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/persons");
dataSource.setUsername("root");
dataSource.setPassword("123192");
// Inject the datasource into the dao
dao.setDataSource(dataSource);
dao.create("Lars", "Vogel");
dao.create("Jim", "Knopf");
dao.create("Lars", "Man");
dao.create("Spider", "Man");
System.out.println("Now select and list all persons");
List<Person> list = dao.selectAll();
for (Person myPerson : list) {
System.out.print(myPerson.getFirstName() + " ");
System.out.println(myPerson.getLastName());
}
System.out
.println("Now select and list all persons with have the firstname Lars and lastname Vogel");
list = dao.select("Lars", "Vogel");
for (Person myPerson : list) {
System.out.print(myPerson.getFirstName() + " ");
System.out.println(myPerson.getLastName());
}
// Clean-up
dao.deleteAll();
}
}
这是Dao类
package dao;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import dao.mapper.PersonRowMapper;
import domainmodel.Person;
public class DerbyDao implements IDao {
private DataSource dataSource;
public void setDataSource(DataSource ds) {
dataSource = ds;
}
public void create(String firstName, String lastName) {
JdbcTemplate insert = new JdbcTemplate(dataSource);
insert.update("INSERT INTO PERSON (FIRSTNAME, LASTNAME) VALUES(?,?)",
new Object[] { firstName, lastName });
}
public List<Person> select(String firstname, String lastname) {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select
.query(
"select FIRSTNAME, LASTNAME from PERSON where FIRSTNAME = ? AND LASTNAME= ?",
new Object[] { firstname, lastname },
new PersonRowMapper());
}
public List<Person> selectAll() {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select.query("select FIRSTNAME, LASTNAME from PERSON",
new PersonRowMapper());
}
public void deleteAll() {
JdbcTemplate delete = new JdbcTemplate(dataSource);
delete.update("DELETE from PERSON");
}
public void delete(String firstName, String lastName) {
JdbcTemplate delete = new JdbcTemplate(dataSource);
delete.update("DELETE from PERSON where FIRSTNAME= ? AND LASTNAME = ?",
new Object[] { firstName, lastName });
}
}
包dao;
导入java.util.List;
导入javax.sql.DataSource;
导入org.springframework.jdbc.core.jdbc模板;
导入dao.mapper.PersonRowMapper;
导入domainmodel.Person;
公共类DerbyDao实现了IDao{
私有数据源;
public void setDataSource(数据源ds){
数据源=ds;
}
public void create(String firstName、String lastName){
JdbcTemplate insert=新的JdbcTemplate(数据源);
insert.update(“插入个人(姓氏、姓氏)值(?,)”,
新对象[]{firstName,lastName});
}
公共列表选择(字符串名、字符串名){
JdbcTemplate select=新的JdbcTemplate(数据源);
返回选择
.查询(
“从FIRSTNAME=?和LASTNAME=?”中选择FIRSTNAME、LASTNAME”,
新对象[]{firstname,lastname},
新PersonRowMapper());
}
公共列表selectAll(){
JdbcTemplate select=新的JdbcTemplate(数据源);
返回select.query(“从PERSON中选择FIRSTNAME、LASTNAME”,
新PersonRowMapper());
}
public void deleteAll(){
JdbcTemplate delete=新的JdbcTemplate(数据源);
删除。更新(“从个人删除”);
}
public void delete(String firstName、String lastName){
JdbcTemplate delete=新的JdbcTemplate(数据源);
delete.update(“从FIRSTNAME=?和LASTNAME=?”的人处删除”,
新对象[]{firstName,lastName});
}
}
我刚在线得到这个,但我尝试将其转换为MySql数据库,而不是Derby数据库看起来您没有“person”数据库:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'person'
虽然您的连接字符串指定了“persons”表,但带有“s”。在某处输入错误?首先检查您是否正确连接到数据库。为它创建一个类。然后检查名为“person”的表是否存在
你能给我们看一下你的带注释的
人类吗?(或您的Hibernate XML文件)以及您的DAO类-为什么它被称为DerbyDao
?@millhouse:看起来像是一个连接,而不是一个映射问题。Nialscorva的答案是正确的。我没有Hibernate XML文件,这是一个独立的java应用程序,请重新检查我的代码我已经更新了itI我已经将我的表名更改为persons,很好alreadyDid您是否成功运行了MysqlConnect类?我更改了它,是的,我用Payroll替换了我的数据库名,我一直叫错名字,谢谢大家!在命令行中,运行“mysql-u$user-p persons”,然后键入“insert-into-person(firstname,lastname)值('John','Doe')”,看看它是否有效。
package dao;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import dao.mapper.PersonRowMapper;
import domainmodel.Person;
public class DerbyDao implements IDao {
private DataSource dataSource;
public void setDataSource(DataSource ds) {
dataSource = ds;
}
public void create(String firstName, String lastName) {
JdbcTemplate insert = new JdbcTemplate(dataSource);
insert.update("INSERT INTO PERSON (FIRSTNAME, LASTNAME) VALUES(?,?)",
new Object[] { firstName, lastName });
}
public List<Person> select(String firstname, String lastname) {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select
.query(
"select FIRSTNAME, LASTNAME from PERSON where FIRSTNAME = ? AND LASTNAME= ?",
new Object[] { firstname, lastname },
new PersonRowMapper());
}
public List<Person> selectAll() {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select.query("select FIRSTNAME, LASTNAME from PERSON",
new PersonRowMapper());
}
public void deleteAll() {
JdbcTemplate delete = new JdbcTemplate(dataSource);
delete.update("DELETE from PERSON");
}
public void delete(String firstName, String lastName) {
JdbcTemplate delete = new JdbcTemplate(dataSource);
delete.update("DELETE from PERSON where FIRSTNAME= ? AND LASTNAME = ?",
new Object[] { firstName, lastName });
}
}
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'person'
import java.sql.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "persons";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "123192";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}