-
April 7th, 2015, 05:42 PM
#1
Expressing variables in sql statement transfer to database Java(version 8 update31)
Hi there Im new to this forum so I hope I am doing everything ok for you let me know if something needs changing.
Basically Ive been at this code for hours, Im trying to write a class that will accept two strings and an int (username and password and scrore from a quizgame,which eventually will come from a gui, at the minute im just passing them through from the main), and pass them into a database to be inserted.
I have the JConnector jar file added and am working in eclipse, i am really under pressure for time.
here is my code
Sorry it is hard to make it neat here.
Code:
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class DbConnect {
private java.sql.Connection con;
private java.sql.Statement st;
private ResultSet rs;
public DbConnect()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/nostalgic","root","usbw");
st = con.createStatement();
}
catch(Exception ex)
{
System.out.println("Error is "+ex);
}
}
public void setData(String n,String p,int x)
{
try{
String query ="select * from nostalgic";
String query1="INSERT INTO nostalgic values (n,p,x)";
PreparedStatement statement3 = con.prepareStatement(query1);
statement3.executeUpdate();
rs = st.executeQuery(query);
System.out.println("records from database");
while(rs.next())
{
String name1 = rs.getString("name");
String pw = rs.getString("password");
int score = rs.getInt("score");
System.out.println("Name : "+name1);
System.out.println("Password : "+pw);
System.out.println("Score : "+score);
}
}
catch(Exception ex){
System.out.println(ex);
}
}
}
the error i get is Unknown column 'n' in 'field list'
I can directly put a string in like 'john' but that is no use to me in this situation.
Please help
Thank you,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|