使用java将数据插入到openfire数据库

时间:2014-05-17 09:37:20

标签: java mysql jdbc

我正在为我的openfire服务器使用外部Mysql数据库,我试图将数据插入到我的" openfire"命名数据库,其表名是" ofuser"及其字段列是"用户名" ," plainpassword"," encryptedpassword"," name"," email"," creationdate","时间modificationdate"

这是我的代码:

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)

2 个答案:

答案 0 :(得分:1)

您的课程路径中未包含mysql.jar

按照以下步骤

  1. 右键点击您的项目
  2. 建立路径 - &gt;配置构建路径
  3. 点击图书馆标签
  4. 单击添加外部jar并提供mysql.jar
  5. 的路径

答案 1 :(得分:1)

从这里http://www.mysql.com/products/connector/下载jdbc中的相应连接器,然后将下载的jar文件添加到项目库中。