Hello all I joined this forum today
Hello I joined newly in this forum today. Presently I am fiddling with advanced Java concepts. I am trying to handle databases through Java program. I could connect to mysql database, insert into table and query it. Now I want to create a table in database by passing the name of the table as a parameter. My code to do this is as follows:
Code:
public void createTable(String tableName){
connectToDB();
String statement = "create table ?(loginname varchar(12),rollnum varchar(15),stuname varchar(20),course char(4),sem char(2))";
try{
PreparedStatement st = conn.prepareStatement(statement);
st.setString(1,tableName);
st.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}
}
end of code:
Now when I give table name in the statement it works fine and creates table. But when I give it as a parameter I get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''student'(loginname varchar(12),rollnum varchar(15),stuname varchar(20),course c' at line 1
Can somebody tell me how to do it, if it is possible.