【问题标题】:java connectivity problems from netbeans to mysql从netbeans到mysql的java连接问题
【发布时间】:2015-07-06 09:28:17
【问题描述】:

先生/妈妈 这是我的代码成功运行但没有在服务器内插入任何值

输出:-

successfully inserted 11111111
drver loaded successfully
created the connection from the database
successfully inserted

代码是:-

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package examples;

import java.sql.*;
/**
 *
 * @author Rahul_R_
 */
public class newfile {

   Connection conn=null;

    public newfile() {

        //inside no paramerter constructor create inside it to define username,password,url
        String username="root";
        String password="root";
        String url="jdbc:mysql://localhost:3306/login_page";

        try{

            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("drver loaded successfully");


            //create the connection form the database
            conn=DriverManager.getConnection(url,username,password);

            System.out.println("created the connection from the database");


        }catch(ClassNotFoundException ce){

            ce.printStackTrace();
            System.out.println("class not found driver error");


            //any sql error in database connection from sql sides than this exceptions
        }catch(SQLException q){

            q.printStackTrace();
            System.out.println("sql exception in the program or jdbc connectivity");


        }


    }

public void create(Beans_login b){

    //creat the statement to insert something in to the database system

    try{

        //create query written here
        String query="insert into login_info(id,username,password)"+"values(?,?,?)";

        //create the preparestatment and run the query on the tables

        PreparedStatement psmt=conn.prepareStatement(query);

        //according to ur database table insert to written statement

        psmt.setInt(1, b.getId());
        psmt.setString(2, b.getUsername());
        psmt.setString(3, b.getPassword());

    }catch(SQLException e){

        //any sql error to show inside it

        e.printStackTrace();
        System.out.println("inside it any username or table name different than error");
    }

}

//检查连接是否正常

public static void main(String[] args) {

    //to insert the values
    Beans_login b=new Beans_login(4, "ggg", "ggg");
    System.out.println("successfully inserted 11111111");
    //create daos objects
    newfile f=new newfile();
    f.create(b);
    System.out.println("successfully inserted");
    }


}

请给我一个解决方案

【问题讨论】:

    标签: java mysql netbeans


    【解决方案1】:

    您正在创建语句,但从未执行它。请查看this 教程,了解有关如何使用准备好的语句和 Java DB 连接的更多信息。

    psmt.setInt(1, b.getId());
    psmt.setString(2, b.getUsername());
    psmt.setString(3, b.getPassword());
    psmt.executeUpdate(); // This will pass your prepared statement to the DB engine.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2011-05-01
      • 2013-07-29
      • 1970-01-01
      相关资源
      最近更新 更多