我正在尝试让这个javaweb应用程序插入到数据库中。我得到的错误是注意到下面的图片。
问题:我打电话的时候 insertDB()
它工作是因为id列正在计数1,2,3。。。但里面的其他数据 customers36
当我把所有的数据都传进来时,表都是空的。这个 display()
方法工作,但插入没有和我用完的东西,它可以。
显示数据未进入数据库的图像。
注意:selectdb()//也可以
尽量说得具体点。数据库中的数据是除id列以外的所有字符串。所以我把所有的字符串都发送到数据库。
//Code is for a java bank application.
package Business;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author DEVGRU
*/
public class Customer
{
//properties
private String custId;
private String custPassword;
private String custFname;
private String custLname;
private String address;
private String email;
Connection con = null;
//Constructors
public Customer()
{
custId = "";
custPassword = "";
custFname = "";
custLname = "";
address = "";
email = "";
}
public Customer( String id, String pw, String fn, String ln, String add, String em )
{
custId = id;
custPassword = pw;
custFname = fn;
custLname = ln;
address = add;
email = em;
}
//Setters and getters.
public void setCustId( String id )
{
custId = id;
}
public String getCustId()
{
return custId;
}
public void setCustPassword( String pw )
{
custPassword = pw;
}
public String getCustPassword()
{
return custPassword;
}
public void setCustFirstName( String fn )
{
custFname = fn;
}
public String getCustFirstName()
{
return custFname;
}
public void setCustLastName( String ln )
{
custLname = ln;
}
public String getCustLastName()
{
return custLname;
}
public void setAddress( String add )
{
address = add;
}
public String getAddress()
{
return address;
}
public void setEmail( String em )
{
email = em;
}
public String getEmail()
{
return email;
}
//Db Management and Manipulation methods
/**
* @param id@throws ClassNotFoundException
*/
public void selectDb( String id ) throws ClassNotFoundException
{
Class.forName( "com.mysql.jdbc.Driver" );
try
{
//Get the connection and stick it in con.
con = DriverManager.getConnection( Settings.mysql_connstring, Settings.mysql_user, Settings.mysql_password );
Statement stmt = con.createStatement();
String sql; //Single Quotes Arround String Data.Pay Attention.
sql = "Select * from `JavaChatBankDB`.`Customers` where CustID = '" + id + "'";
System.out.println( sql );
ResultSet rs;
//Execute
rs = stmt.executeQuery( sql );
//Process
while ( rs.next() )
{
custId = rs.getString( 1 );
custPassword = rs.getString( 2 );
custFname = rs.getString( 3 );
custLname = rs.getString( 4 );
address = rs.getString( 5 );
email = rs.getString( 6 );
}//End while
}
catch ( SQLException ex )
{
System.out.println( "Error somewhere. " + ex );
}
finally
{
try
{
con.close();
}
catch ( SQLException ex )
{
Logger.getLogger( LoginServlet.class.getName() ).log( Level.SEVERE,
null, ex );
}//End Try/Catch on connection.close() -- sqlexception.
}//End Finally
}//End Select Db
public void insertDB( String id, String pw, String fn, String ln, String add,
String em ) throws ClassNotFoundException
{
Class.forName( "com.mysql.jdbc.Driver" );
try
{
//Get the connection and stick it in con.
con = DriverManager.getConnection( Settings.mysql_connstring, Settings.mysql_user, Settings.mysql_password );
Statement stmt = con.createStatement();
//Single Quotes Arround String DataPay Attention.
String sql = "INSERT INTO `JavaChatBankDB`.`Customers36` (`CustId`,`CustPassword`, `CustFirstName`, `CustLastName`, `CustAddress`, `CustEmail`)"
+ "VALUES ('" + custId + "','" + custPassword + "','" + custFname + "','" + custLname + "','" + address + "','" + email + "')";
System.out.println( sql );
int answer = stmt.executeUpdate( sql );
if ( answer >= 1 )
{
System.out.println( "Success on Inserting Data into the Database. Go Check!!!" );
}
else
{
System.out.println( "An error occured while attempting to update database" );
}
custId = id;
custPassword = pw;
custFname = fn;
custLname = ln;
address = add;
email = em;
}
catch ( SQLException ex )
{
System.out.println( "Error somewhere. " + ex );
}
finally
{
try
{
con.close();
}
catch ( SQLException ex )
{
Logger.getLogger( LoginServlet.class.getName() ).log( Level.SEVERE, null, ex );
}//End Try/Catch on connection.close() -- sqlexception.
}//End Finally
}//End Select Db
public void display()
{
System.out.println( "_________Display___________" );
System.out.println( " " );
System.out.println( "Customer Id: " + custId );
System.out.println( "Customer Password:" + custPassword );
System.out.println( "Customer First Name: " + custFname );
System.out.println( "Custoner Last Name: " + custLname );
System.out.println( "Customer Address: " + address );
System.out.println( "Customer Email: " + email );
}
public static void main( String[] args ) throws ClassNotFoundException
{
//Customer c1 = new Customer("4567","root","mike","baules","afghanistan","camels@sandbox.org");
Customer c2 = new Customer();
//c2.selectDb("3006");
c2.insertDB( "3007", "apple", "Steve", "Jobs", "cali", "apple@apple.com" );
c2.display();
}
}//End Class
2条答案
按热度按时间piwo6bdm1#
代码应该分为多个类,例如dbhelper和customer。请更具体地回答您的问题,因为我只看到您插入一个id为3007类型字符串的客户?还可以考虑对id使用long或atomiclong。
gojuced72#
在发送查询之后,您正在设置查询的值。好吧,那太晚了,查询已经发送了(使用默认值或以前的值)。所以不是:
你要么交换顺序要么不使用变量
custId
,custPassword
,并使用方法参数值id
,pw
,等等。此外,对于带有变量输入的查询,应该使用prepared语句来防止任何sql注入。请阅读https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 如何在java中使用jdbc编写的语句。