note: I use mysql database if that's of relevance
Here's the code:
Can I know how to fix the error
package databaseTest;
// import package
import java.sql.*;
public class DemoClass {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// load package from properties then add mysql jar drive
String url = "jdbc:mysql://localhost:3306/coursesdatabase";
String uname = "root";
String pass = "fatima2002";
String query = "select * from courseslist";
// register driver
Class.forName("com.mysql.cj.jdbc.Driver");
// establish the connect
Connection con = DriverManager.getConnection(url, uname, pass);
// create statement
Statement st = con.createStatement();
// execute query
ResultSet rs = st.executeQuery(query);
String userData=null;
while(rs.next()) { // how to fetch the whole table
// shift pointer to next result
userData = rs.getInt(1) + " : " + rs.getString(2);
// process results
System.out.println(userData);
}
String query2 = "insert into courseslist values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
int count = st.executeUpdate(query2); // affects a row
System.out.println(count + "row/s affected");
int classcode = 24590;
String courseCode = "GENS";
String courseName = "Arabic Language";
int crdhrs = 3;
int sectionNum = 1;
String days = "TRU";
String time = "11:00-11:50";
String instructor = "Hakim Ajhar";
int capacity = 40;
String SemSrtEnd = "28/08-20/12";
String roomNum = "G098549";
PreparedStatement pSt = con.prepareStatement(query2);
pSt.setInt(1, classcode);
pSt.setString(2, courseName);
pSt.setString(3, courseCode);
pSt.setInt(4, crdhrs);
pSt.setInt(5, sectionNum);
pSt.setString(6, days);
pSt.setString(7, time);
pSt.setString(8, instructor);
pSt.setInt(9, capacity);
pSt.setInt(10, 0);
pSt.setInt(11, 0);
pSt.setString(12, SemSrtEnd);
pSt.setString(13, roomNum);
int count2 = pSt.executeUpdate();
System.out.println(count2 + "row/s affected");
// close
st.close();
con.close();
}
}