MySQL和Java:表格不存在 [英] MySQL and Java: table doesn't exist

查看:265
本文介绍了MySQL和Java:表格不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySQL Workbench 5.2.40 CE创建了MySQL数据库.首先,我创建了新的EER模型并将其命名为test.然后创建名为:testdb的架构,然后创建名为:table1的表.另外,创建一个连接.

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.

以下与我的数据库建立连接的类.成功

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

然后,我制作了另一个类,其中包含Main函数,该函数从上述"DBConnection"类中调用"ConnectDB".问题是我收到一条错误消息: 有一个例外! 表'test.table1'不存在

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

我确定table1存在.我从工作台复制了它的名字.

I'm sure that table1 exists. I copied its name from the workbench.

这是Main类的代码:

This is the code for the Main class:

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());
} 
}
}

那么,如果表存在并且我在Java和MySQL中使用了所有小写字母,那么问题出在哪里呢?我通过仅在Main中调用"ConnectDB"函数并在我的"DBConnection"类中将连接类型的对象定义为公共静态对象来做正确的事情吗?稍后,我将在许多类中插入值??对于这个问题,我们感到很抱歉,但是我一直在搜索很多东西,并希望确保自己做对了,这是我的第一个连接数据库的Java应用程序.

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.

编辑

推荐答案

两种选择:

  1. 连接到testdb
  2. 使用数据库限定符访问表.

连接到testdb :

con = DriverManager.getConnection
     ("jdbc:mysql://localhost:3306/testdb", "root", "password");

使用数据库限定符:

query = " insert into testdb.table1 (name)"+ " values (?)";

这篇关于MySQL和Java:表格不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆