gpt4 book ai didi

java - 使用java向openfire数据库插入数据

转载 作者:行者123 更新时间:2023-11-29 08:01:24 25 4
gpt4 key购买 nike

我正在为我的 openfire 服务器使用外部 Mysql 数据库,并且我尝试将数据插入到我的“openfire”命名数据库中,该数据库的表名称为“ofuser”,其字段列为“username”、“plainpassword”, “加密密码”,“姓名”,“电子邮件”,“创建日期”,“修改日期”

这是我的代码:

try{
Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection
System.out.println("Connecting to a selected
database...");
conn = DriverManager.getConnection(DB_URL, USER,
PASS);
System.out.println("Connected database
successfully...");

//STEP 4: Execute a query
System.out.println("Inserting records into the
table...");
stmt = conn.createStatement();

String sql = "INSERT INTO ofuser " +
"VALUES (100, 'Zara', 'Ali', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ofuser " +
"VALUES (101, 'Mahnaz', 'Fatma', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ofuser " +
"VALUES (102, 'Zaid', 'Khan', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO ofuser " +
"VALUES(103, 'Sumit', 'Mittal', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the
table...");

}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
display("End of DB");

}

我使用 eclipse 运行它。但什么也没发生,我哪里错了?

更新:

我在控制台中收到此错误:

  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Server$ClientThread.<init>(Server.java:236)
at Server.start(Server.java:74)
at ServerGUI$ServerRunning.run(ServerGUI.java:132)

最佳答案

您尚未包含mysql.jar在你的类路径中。

请按照以下步骤操作

  1. 右键单击您的项目
  2. 必须构建路径 -> 配置构建路径
  3. 点击“库”选项卡
  4. 点击添加外部jar并给出mysql.jar的路径

关于java - 使用java向openfire数据库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709796/

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