I want my function to return the result of the Select statement, but in instead it prints SQLServerResultSet:1. How do I do to print/return the data in my function?
My function should execute the SQL statement and store the result in a variable.
public ResultSet selectSubmissions(int department) {
Connection connection = Database.getConnection();
String sql = "SELECT * FROM [MyDB_Widget].[dbo].[surveys] INNER JOIN [MyDB_Widget].[dbo].[instructors] ON surveys.instructor_id = instructors.instructor_id Where instructors.department_id = 2;";
ResultSet rs = null;
try {
Statement statement = connection.createStatement();
rs = statement.executeQuery(sql);
while (rs.next()) {
rs.getString(1); //or rs.getString("column name");
}
} catch (SQLException e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
return rs;
}
Related
I can't add items to the database because of this apparently.
Not putting the entire code, hope you can understand.
import java.sql.Connection/Driver/ResultSet/SQLException/Statement;
public class Main {
public static void main(String[] args) {
Statement st = null;
ResultSet rs = null;
try(Connection conn = everything right here)){
if(conn != null) {
System.out.println("Success conn!");
String query = "INSERT INTO students (id, nome) VALUES (1, 'College Name')";
st = conn.createStatement();
rs = st.executeUpdate(query);
Giving me type mismatch: can't resolve int to ResultSet in
rs = st.executeUpdate(query);
// continuation of above code
while(rs.next()) {
System.out.println("\nRegistro");
System.out.println("id: " + rs.getInt(1));
System.out.println("nome: " + rs.getString(2));
}
rs.close();
st.close();
conn.close();
}
}
}
}
I want to retrieve all the name and the number of row from MySQL to java. So far I only able to retrieve the total row number but I only get the last name. What's wrong here ?
StaffManagement.java
adminAPI api= new adminAPI();
try {
int num= api.displayCheckBoxAndLabel();
String allName= api.displayName();
System.out.println(num+allName);
}
adminAPI
public int displayCheckBoxAndLabel() throws Exception // get the number of row
{
int count = 0;
String sql="Select count(*) AS adminID from admin";
DatabaseConnection db = new DatabaseConnection();
Connection conn =db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
count= rs.getInt("adminID");
}
ps.close();
rs.close();
conn.close();
return count ;
}
public String displayName() throws Exception // get all the name
{
String name = null;
String sql="Select name from admin";
DatabaseConnection db = new DatabaseConnection();
Connection conn =db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
name= rs.getString("name");
}
ps.close();
rs.close();
conn.close();
return name ;
}
You currently return a single String, and your method iterates all of the admin names (but terminates after the final row, so that's your result). Instead, build a List of names and return that. You could also use a try-with-resources close to close your Connection, Statement and ResultSet instances. Something like
public List<String> displayName() throws Exception // get all the name
{
String sql = "Select name from admin";
List<String> names = new ArrayList<>();
DatabaseConnection db = new DatabaseConnection();
try (Connection conn = db.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
names.add(rs.getString("name"));
}
}
return names;
}
This might be helpful
private String names[];
int i = 0;
while (rs.next()) {
names[i] = rs.getString("name");
i++;
}
Then you can use a for loop to return each name in StaffManagement.java
Example:
String s1;
String q = "select * from EntryByTitle where booktitle='"+s1+"'";
here in query statement why +s1+ is used in the syntax. As s1 is string, so it should be '"s1"'. But why '"+s1+"' is written in project.
Use a PreparedStatement and a bind parameter. This usually takes the form
String q = "select * from EntryByTitle where booktitle=?";
String bookTitle = "";
Connection conn = null;
try {
try (PreparedStatement ps = conn.prepareStatement(q)) {
ps.setString(1, bookTitle);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString("booktitle"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
hello i am trying to retrieve data in two result set. then i have store in both resultset to String Array, after this process i have to store my String array to another table how to so this kindly tell me..
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.
getConnection("jdbc:mysql://localhost:3306/lportal", "root", "ubuntu123");
PreparedStatement stmt = (PreparedStatement) con.
prepareStatement("SELECT * FROM Testi_Testimonial where subject = ?");
stmt.setString(1, search);
stmt.execute();
rs = (ResultSet) stmt.getResultSet();
while (rs.next()) {
anArray[i] = rs.getString("subject");
System.out.println(anArray[i]);
i++;
count++;
}
PreparedStatement stmt1 = (PreparedStatement) con.
prepareStatement("SELECT * FROM Testi_Testimonial where subject != ?");
stmt1.setString(1, search);
stmt1.execute();
rs1 = (ResultSet) stmt1.getResultSet();
while (rs1.next()) {
anArray[i] = rs1.getString("subject");
System.out.println(anArray[i]);
i++;
count++;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("problem in connection");
}
Please tell me how should I store my array to Temp table? my "Temp" table has column subject I want to store myArray to subject column... kindly tell me how to do this.
try this after getting resultset in rs:
PreparedStatement st = (PreparedStatement) con.prepareStatement("insert into temp values(?)");
i=0;
while(rs.next())
{
st.setString(1,anArray[i]);
st.executeUpdate();
i++;
}
You just iterate the arrayList. You got the subject List as respective.
public class TestArrayList {
public static void main(String[] args) {
/*create two arrayList*/
List<String> tempOneList = new ArrayList<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vijay", "root", "root");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM subjecttable");
while (res.next()) {
tempOneList.add(res.getString("subject"));
}
ResultSet resOne = st.executeQuery("SELECT * FROM subjecttabletwo");
while (resOne.next()) {
tempOneList.add(resOne.getString("subject"));
}
System.out.println("temp/List>>--" + tempOneList.size());
for(int i=0;i<tempOneList.size();i++){
System.out.println(tempOneList.get(i));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("problem in connection");
}
}
}
PreparedStatement ps =
(PreparedStatement) con.prepareStatement("insert into temp values(?)");
j=0;
while(rs.next())
{
ps.setString(1,anArray[j]);
ps.executeUpdate();
j++;
}
I am getting an exception like as
java.sql.SQLException: Before start of result set when calling getInt() method
My code is as follows:::
public void setdateTOSource(){
try{
String q = "SELECT * FROM ipp.resource;";
ResultSet result = DB_Access.getData(q);
System.out.println("Size result set"+result.getFetchSize());
while(result.next()){
getIpSource().add(result.getString(1));
System.out.println(result.getString(1));
}
System.out.println("source size "+getIpSource().size());
}
catch(Exception e){
System.out.println(e);
}
}
public void insertTargetToDb(List<String> targetValue){
try{
Connection con = DB_Connect.getDataBaseConnection();
Statement statement1 = con.createStatement();
int val=0;
String q1 = "SELECT MAX(area_id) as aa FROM ipp.area;";
ResultSet resultset = statement1.executeQuery(q1);
if(resultset.next()){
val = resultset.getInt(1);
};
val++;
String q2 = "INSERT INTO ipp.area VALUES("+val+",'"+getAreaName()+"',1);";
Statement statement2 = con.createStatement();
statement1.executeUpdate(q2);
for( int y=0;y<targetValue.size();y++){
System.out.println("FS");
String ip=targetValue.get(y);
System.out.println("ip" +ip);
String q3 ="SELECT resource_id FROM ipp.resource r where ip_address like '"+ip+"%';";
Statement statement3 = con.createStatement();
ResultSet resultset3 =statement3.executeQuery(q3);
System.out.println("FS1");
// ResultSet result1 =DB_Access.getData(q3);
System.out.println("FS2");
System.out.println("result set"+resultset3.getFetchSize());
//====================================================================
while(resultset3.next()){
setId(resultset.getInt(1));
}
System.out.println("result :........."+resultset3.getInt("resource_id"));
System.out.println("FS3");
String q4 = "INSERT INTO ipp.used_resource VALUES("+val+","+getId()+");";
Statement statement4= con.createStatement();
statement4.executeUpdate(q4);
// DB_Access.setData(q3);
statement3.close();
statement4.close();
// resultset3.close();
}
resultset.close();
statement1.close();
statement2.close();
con.close();
addMessage("Area created Successfully");
}
catch(Exception e){
System.out.println(e);
}
}
Exception comes after line
//==========================
resource_id is an integer value
This line
while(resultset3.next()){
setId(resultset.getInt(1));
}
must be replaced with?
while(resultset3.next()){
setId(resultset3.getInt(1));
}