gpt4 book ai didi

java - 无法从java应用程序在数据库中创建表

转载 作者:行者123 更新时间:2023-11-29 14:08:50 26 4
gpt4 key购买 nike

这是我的代码..刚刚从 mySQL 开始。 IDE是netbeans 7.2.1

    package monitordata;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.xml.ws.Endpoint;


public class monitorDataMF {

public static void main(String[] args){
Statement stmt = null;
String dbname = null;
String dbuser = null;
String dbpass = null;
String dbport = null;
String dbIP = null;

//pairnoume tis parametrous gia ti sindesi me ti vasi apo to properties file
try{
Properties prop = new Properties();
prop.load(new FileInputStream("mySQL.properties"));
dbname = prop.getProperty("dbname");
dbuser = prop.getProperty("dbuser");
dbpass = prop.getProperty("dbpass");
dbport = prop.getProperty("dbport");
dbIP = prop.getProperty("dbIP");
}catch(IOException e){e.printStackTrace();}

//connecting with database
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://" + dbIP + ":" + dbport + "/" + dbname;
conn = DriverManager.getConnection(dbUrl, dbuser, dbpass);
String createTable = "CREATE TABLE IF NOT EXISTS test"
+ "(NAME VARCHAR(40) NOT NULL)";
//String insertData = "INSERT INTO test "
// + "VALUES ('Stelios','Thwmas')";
stmt = conn.createStatement();
stmt.executeUpdate(createTable);
//stmt.executeUpdate(insertData);
}catch(ClassNotFoundException | SQLException e){}
}
}

我执行更新,我转到服务>WebInterfaces我点击刷新,但 table 不在那里。我不确定是否应该执行更新或执行查询

更新: 这是实际的错误 捕获异常:用户“root”@“localhost”访问被拒绝(使用密码:YES) 但我打印了发送到连接语句的通行证,它与数据库启动所需的通行证相对应。不确定我做错了什么

最佳答案

您可能会在执行时遇到错误,因为您没有在catch语句中记录异常。修改代码看看错误。您还必须正确关闭连接。

   Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://" + dbIP + ":" + dbport + "/" + dbname;
conn = DriverManager.getConnection(dbUrl, dbuser, dbpass);
String createTable = "CREATE TABLE IF NOT EXISTS test"
+ "(NAME VARCHAR(40) NOT NULL)";
//String insertData = "INSERT INTO test "
// + "VALUES ('Stelios','Thwmas')";
stmt = conn.createStatement();
stmt.executeUpdate(createTable);
//stmt.executeUpdate(insertData);
}catch(ClassNotFoundException exp){
System.err.println("Caught IOException: " + exp.getMessage());
}catch( SQLException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if(conn != null) {
conn.close();
}
}

关于java - 无法从java应用程序在数据库中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com