首页 新闻 会员 周边

用eclipse对数据库进行操作,提示表不存在

0
悬赏园豆:50 [已解决问题] 解决于 2017-07-27 09:55

本人小白,都是对着书上敲代码。实在是解决不了了-_-

package MysqlTest;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;


public class PreparedStatementTest {
private String driver;
private String url;
private String user;
private String password;
public void initParam(String paramFile)throws Exception
{
Properties props=new Properties();
props.load(new FileInputStream(paramFile));
driver=props.getProperty("driver");
url=props.getProperty("url");
user=props.getProperty("user");
password=props.getProperty("password");
}
public void insertUseStatement() throws Exception//这一个statement方法可以正常调用
{
long start=System.currentTimeMillis();
try(Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement())
{
for(int i=0;i<100;i++)
{
stmt.executeUpdate("insert into student_table values("+i+",'cmd',"+i+")");
}
}
System.out.println("使用Statement费时:"+(System.currentTimeMillis()-start));
}
public void insertUsePrepare() throws Exception//这一个出错了,提示表不存在。可是我是和上面那个一模一样的呀。怎么不存在呢
{
long start=System.currentTimeMillis();

try(Connection conn=DriverManager.getConnection(url,user,password);
PreparedStatement pstmt = conn.prepareStatement("insert into student_test values(250,?,1)"))
{
for(int i=0;i<100;i++)
{
pstmt.setString(1,"cmd");
pstmt.executeUpdate();
}
System.out.println(start);
}
System.out.println("使用PreparedStatement费时:"+(System.currentTimeMillis()-start));
}
public static void main(String[] args) throws Exception
{
PreparedStatementTest pt=new PreparedStatementTest();
pt.initParam("db.properties");
pt.insertUseStatement();
pt.insertUsePrepare();
}
}

 

 

错误提示:

使用Statement费时:258
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'xianxian.student_test' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2494)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at MysqlTest.PreparedStatementTest.insertUsePrepare(PreparedStatementTest.java:48)
at MysqlTest.PreparedStatementTest.main(PreparedStatementTest.java:59)

 

好奇他怎么调出来xianxian.student_test表的。

 

我的表相关属性:

耳朵嫁给了真正的谎的主页 耳朵嫁给了真正的谎 | 初学一级 | 园豆:195
提问于:2017-07-27 09:26
< >
分享
最佳答案
0

数据库打开连接了吗,再仔细看看是不是有什么地方漏掉了

收获园豆:50
青语 | 小虾三级 |园豆:1000 | 2017-07-27 09:39

我在网上找了一个执行查询表的命令,也发现我压根没那个表。可是要是我数据库没打开应该两个都执行不了才对呀

 

-----执行查询所有表的查询语句------
jdbc_test

my_test

student_table

teacher_table

使用Statement费时:23
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'xianxian.student_test' doesn't exist

 

耳朵嫁给了真正的谎 | 园豆:195 (初学一级) | 2017-07-27 09:43

@耳朵嫁给了真正的谎: 换个表名再试试

青语 | 园豆:1000 (小虾三级) | 2017-07-27 09:47

@青语: 解决了,但是好懵呀,就这样~吗,居然可以了

耳朵嫁给了真正的谎 | 园豆:195 (初学一级) | 2017-07-27 09:55

@耳朵嫁给了真正的谎: 你的“xianxian.student_test”  这个表名系统会认为是xianxian数据库中的student_test表,但你的数据库并没有student_test这个表,所以这个表名是不规范的。  

青语 | 园豆:1000 (小虾三级) | 2017-07-27 10:01

@青语: 那个你能再帮我看看这一块吗?我在MYSQL语句运行时对的。

int result=ed.insertData("insert into jdbc_test(jdbc_name,jdbc_desc)"
+"select student.name,teacher_table.name"
+"from student,teacher_table "
+" where student_table.java_teacher = teacher_table.id");

 

然后提醒是这个样子的:

-----执行查询所有表的查询语句------
jdbc_test

my_test

student

student_table

teacher_table

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'student' in field list

耳朵嫁给了真正的谎 | 园豆:195 (初学一级) | 2017-07-27 10:13

@耳朵嫁给了真正的谎:  你的数据库里面没有student 这个表吧

青语 | 园豆:1000 (小虾三级) | 2017-07-27 10:18

@青语: 有呀,我上面查询所有表都查询出来了

呀。要是没有我mysql也不会通过呀~~~

耳朵嫁给了真正的谎 | 园豆:195 (初学一级) | 2017-07-27 10:19

@耳朵嫁给了真正的谎: select student.name,teacher_table.name from student,teacher_table  where student_table.java_teacher = teacher_table.id     这是你的查询语句对吧。   感觉你的查询语句写的不对。 你是从  student,teacher_table  这两个表中找各自的name ,但是你的 where  条件却是student_table 表的java_teacher字段

青语 | 园豆:1000 (小虾三级) | 2017-07-27 10:25

@青语: 好的,十分感谢。我已经解决了

耳朵嫁给了真正的谎 | 园豆:195 (初学一级) | 2017-07-28 09:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册