MySQL 和 Java:表不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11244012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL and Java: table doesn't exist
提问by Jury A
I have created MySQL DB using MySQL Workbench 5.2.40 CE. At first, I created New EER Model and named it test. Then created schema named: testdb, then table named: table1. Also, created a connection.
我已经使用 MySQL Workbench 5.2.40 CE 创建了 MySQL DB。起初,我创建了新的 EER 模型并将其命名为 test。然后创建名为:testdb 的模式,然后创建名为:table1 的表。此外,创建了一个连接。
The following class to create connection with my DB. It is successful
以下类用于创建与我的数据库的连接。成功了
import java.sql.*;
public class DBConnection {
public static Connection con = null;
public static Object Connect;
public static void ConnectDB () {
System.out.println("--- MySQL JDBC Connection Testing -----");
try {
Class.forName("com.mysql.jdbc.Driver"); //loading the driver
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
try {
//Connect to the database
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/test", "root", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (con != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
} //end class
Then, I made another class that contains the Main function that calls the "ConnectDB" from the the above "DBConnection" class. The problem is that I get an error saying: Got an exception! Table 'test.table1' doesn't exist
然后,我创建了另一个类,其中包含调用上述“DBConnection”类中的“ConnectDB”的 Main 函数。问题是我收到一条错误消息: 出现异常!表“test.table1”不存在
I'm sure that table1 exists. I copied its name from the workbench.
我确定 table1 存在。我从工作台复制了它的名字。
This is the code for the Main class:
这是 Main 类的代码:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.PreparedStatement;
import java.util.Scanner;
import java.sql.*;
public class Main {
public static void main(String[] argv) {
String name=null;
DBConnection.ConnectDB(); //connect to database
//Read the inputs from the user
System.out.println("Enter the name: ");
Scanner userInput=new Scanner(System.in);
name=userInput.next();
//Confirmation messages
System.out.println("You entered: "+ name);
// BEGIN INSERT TO DB
try{
// the mysql insert statement
String query=null;
query = " insert into table1 (name)"+ " values (?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = DBConnection.con.prepareStatement(query);
preparedStmt.setString (1, name);
// execute the preparedstatement
preparedStmt.execute();
System.out.println("Inserted");
DBConnection.con.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
}
So, where is the problem if the table exists and I used all small letters in Java and MySQL?? and am I doing the right thing by calling the "ConnectDB" function in the Main only and define an object of type connection as public static in my "DBConnection" class?? I'm going to insert values throughout many classes later?? I'm sorry for the question, but I have been searching a lot and want to make sure I got it right, and this is my first Java application that connects to database.
那么,如果表存在并且我在Java和MySQL中使用了所有小写字母,那么问题出在哪里??我是否通过仅在 Main 中调用“ConnectDB”函数并将连接类型的对象定义为“DBConnection”类中的公共静态对象来做正确的事情?稍后我将在许多类中插入值?对于这个问题,我很抱歉,但我一直在搜索并希望确保我做对了,这是我第一个连接到数据库的 Java 应用程序。
EDIT
编辑
回答by Ravinder Reddy
Two alternatives:
两种选择:
- Connect to
testdb
- Access your table using database qualifier.
- 连接到
testdb
- 使用数据库限定符访问您的表。
Connecting to testdb:
连接到 testdb:
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/testdb", "root", "password");
Using database qualifier:
使用数据库限定符:
query = " insert into testdb.table1 (name)"+ " values (?)";
回答by mtariq
You JDBC url is not correct use
jdbc:mysql://localhost:3306/testdb
in your code.
您的 JDBC urljdbc:mysql://localhost:3306/testdb
在您的代码中使用不正确
。