建立的一个数据表,但是在运行时他总报错You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'db' at line 1
Java代码-------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCUtil {
private static final String URL="jdbc:mysql://127.0.0.1:3306/imooc";
private static final String USER="root";
private static final String PASSWORD="123456";
public static void main(String[] args) throws Exception {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库的连接
Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
//3.通过数据库连接操作数据库,实现增删查改
Statement stmt=conn.createStatement();
//查询imooc_goddess里面的user_name,age
ResultSet rs=stmt.executeQuery("select user_name,age form imooc_goddess");
while(rs.next()){//为真,则有数据
System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
}
}
}
数据表:
CREATE TABLE `db` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`sex` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`mobile` varchar(11) DEFAULT NULL,
`create_user` varchar(30) DEFAULT NULL,
`create_date` date DEFAULT NULL,
`update_user` varchar(30) DEFAULT NULL,
`update_date` date DEFAULT NULL,
`isdel` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
SET character_set_client="utf8";
SET character_set_connection="utf8";
SET character_set_results="utf8";
自己手动添加了一条信息