I cant connect my db with my java aplication and I really dont know whts wrong with it, comeone cant help me, please
try {
Connection miconexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/concesionario","root","password");
// 2.PASO CREAR PASO STAMENT.
Statement miStatement = miconexion.createStatement();
//3.PASO EJECUTAR MY SQL.
ResultSet miresulset = miStatement.executeQuery("SELECT * FROM CLIENTES");
//4.PASO RECORRER EL RESULSET
while(miresulset.next()) {
System.out.println(miresulset.getString("dni" + miresulset.getString("nombre" + "" + miresulset.getString("apellido"))));
}
}catch(Exception e) {
System.out.println("NO CONECTA");
e.printStackTrace();
}
This line
System.out.println(miresulset.getString("dni" + miresulset.getString("nombre" + "" + miresulset.getString("apellido"))));
adds whatever is found as apellido to "nombre". The value happens to be "Garcia", hence the String of nombreGarcia in the error. This is as far as I can go with helping you without further information from your part.
Related
Edit: I have apparently solve the problem forcing the code getting the HTML. The problem I have is that randomly the HTML is not taken. To force that I have added:
int intento = 0;
while (document == null) {
intento++;
System.out.println("Intento número: " + intento);
document = getHtmlDocument(urlPage);
}
I am experiencing this random issue. Sometimes it gives me problems when fetching an URL an as it reaches to the timeout the program execution stops. The code:
public static int getStatusConnectionCode(String url) {
Response response = null;
try {
response = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(100000).ignoreHttpErrors(true).execute();
} catch (IOException ex) {
System.out.println("Excepción al obtener el Status Code: " + ex.getMessage());
}
return response.statusCode();
}
/**
* Con este método devuelvo un objeto de la clase Document con el contenido del
* HTML de la web que me permitirá parsearlo con los métodos de la librelia JSoup
* #param url
* #return Documento con el HTML
*/
public static Document getHtmlDocument(String url) {
Document doc = null;
try {
doc = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(100000).get();
} catch (IOException ex) {
System.out.println("Excepción al obtener el HTML de la página" + ex.getMessage());
}
return doc;
}
Should I use another method or increase the time out limit? The problem is that the program execution spends more or less 10 hours, and sometimes the problem happens in the URL number 500 another time in the 250...this is nonsense for me...if there is a problem in the link number 250, why if I run another time the program the problem happens in the link number 450 (for example)? I have been thinking that it could be internet problems but it's not.
The solution for another case is not solving my problem: Java JSoup error fetching URL
Thanks in advice.
What I have attached below is a copy/paste of my attempt to get and set a series of JComboBox's. hName and hAddress are dealing with a JTextField, so those are working fine, but for some reason I can get from my combo box's, but I can't set to them and including a call to initialiseDatabase() will cause the program to fail.
infoHolder is a resultset and dbstatement is a statement. The database is connecting fine, and the table inside of it is prepared and accessible, it's just the setting of the combobox's that's the issue.
Any help appreciated, and apologies if I'm missing something fundamental in the code below above.
public void initialiseDatabase()
{
try
{
infoHolder = dBStatement.executeQuery("SELECT * FROM House");
infoHolder.next();
hName = infoHolder.getString("Name");
hAddress = infoHolder.getString("Address");
numRooms = infoHolder.getInt("numRooms");
Type = infoHolder.getString("Type");
charge = infoHolder.getString("roomCharge");
Access = infoHolder.getString("dAccess");
Info = infoHolder.getString("oInfo");
houseText.setText(" " +hName);
addressText.setText(" " +hAddress);
roomNumber.setSelectedItem(" " +numRooms);
roomType.setSelectedItem(" " +Type);
chargeRoom.setSelectedItem(" " +charge);
access.setSelectedItem(" " +Access);
oInfo.setText(" " +Info);
current2 = 1;
infoHolder.close();
}
catch(SQLException e8) {JOptionPane.showMessageDialog(this, "Database failed to initialise!", "Message Dialog", JOptionPane.INFORMATION_MESSAGE);;}
}
This question already has an answer here:
Avoiding java.lang.IllegalStateException in jsp
(1 answer)
Closed 8 years ago.
<%
String emi1;
String repo1;
String arr1;
String reso1;
String pi1;
emi1=request.getParameter("e"+count);
repo1=request.getParameter("repo"+count);
arr1=request.getParameter("a"+count);
reso1=request.getParameter("r"+count);
pi1=request.getParameter("pi"+count);
if(emi1!=null && repo1!=null && arr1!=null && reso1!=null && pi1!=null)
{
String ReserveDt = request.getParameter("ReserveDate");
System.out.println(emi1);
System.out.println(repo1);
System.out.println(arr1);
System.out.println(reso1);
System.out.println(pi1);
System.out.println(ReserveDt);
String sql=("BEGIN DECLARE #DT DATE;DECLARE #CIR NVARCHAR(2);SET #CIR='"+circle+"' DECLARE #MON NVARCHAR(2); DECLARE #DAY NVARCHAR(2); "
+"DECLARE #YEAR NVARCHAR(4);DECLARE #curr NVARCHAR(50); DECLARE #reg nvarCHAR(3);"
+"set #reg='"+utype+"' SET #DT='"+ReserveDt+"' SET #day=(SELECT DATEPART(day,#DT)) "
+"SET #MON=(SELECT DATEPART(month,#DT)) SET #year=(SELECT DATEPART(year,#DT)) "
+"SET #curr=#reg+convert(nvarchar(2),#DAY)+convert(nvarchar(2),#MON)+convert(nvarchar(4),#YEAR)+CONVERT(NVARCHAR(2),#CIR) "
+"INSERT INTO [DCRA].[dbo].[SAM]([REGION],[CIRCLE],[TOTAL],[EMI],[REPO],[REPO ARREAR],[RES],[CURR_DATE],[PI],[DATAID]) "
+"VALUES (#reg,#CIR,0.24,"+emi1+","+repo1+","+arr1+","+reso1+",'"+ReserveDt+"',"+pi1+",#curr) end");
System.out.println(sql);
stmt=con.createStatement();
stmt.executeUpdate(sql);
System.out.println(sql);
if("stmt"!=null)
{
response.sendRedirect("Add.jsp");
}
}
count++;
System.out.println(count+"my count");
}
}
catch(Exception e)
{
%>
<script type="text/javascript">
alert("Data is alredy insert or please check your data!");
</script>
<%
e.printStackTrace();
}
%>
I am very new into jsp , i am inserting the value into table that time it gives me error. I am showing data is added successfully in add.jsp.how to resolve this problem.please help me to resolve this problem.
i didnt find your try block without try you can not catch proper exception and also
put this line in your code
System.out.println(e.toString());
so that you get proper exception
I'm using the following code to get the response from a servlet. It will check whether the given name in the variable "get" is in a particular table, and print 1 if it exists.
Portion of servlet code:
get = request.getParameter("nam");// such as get="kannan"
try {
// Connection code
ResultSet rs = stmt
.executeQuery("select * from newfarmer where rname='" + get
+ "'");
while (rs.next()) {
username = rs.getString("rname");
if (get.equals(username)) {
out.println(1);
}
}
} catch (Exception e) {
out.println(e.toString());
}
In my android application, I check this response as follows:
response = CustomHttpClient.executeHttpPost(
"http://moberp.svsugar.com:8080/androidservlt/Modify",
postParameters);
String res = response.toString();
res = res.trim();
if (res.equals("1")) {
flag = 1;
Toast.makeText(getBaseContext(), "Correct", Toast.LENGTH_LONG)
.show();
} else {
flag = 2;
Toast.makeText(getBaseContext(), "please enter correct Ryot name",
Toast.LENGTH_LONG).show();
}
It works very well for single record. I mean, in the table "newfarmer", If "rname" consists of more than one same name only the else part is executed.
Example:
If "kannan" is presented 2 times in the table Servlet output is as
1 1
Now in android application, clearly the else part is executed because response is not 1.
This is only case of two same names. The table may contains more than 10 same names.
If 10 same names, then servlet output is as
1 1 1 1 1 1 1 1 1 1
So I need to check all.
So I need to make changes in my if condition, but I don't know what to do. Someone please give answer.
Thanks in advance
instead of while loop Use
if(rs.next())
Now it will print only one time.
No change in android
in servlet do this
if(rs.next())
{
username=rs.getString("rname");
if(get.equals(username))
{
out.println(1);
}
}
}
this loop will run only 1 time now if the record is present.
I don't understand why would get.equals(username) will evaluate to false when you are having a where clause in your SQL query?
So just try this.
if(rs.next())
{
// The above condition will make the code inside if executed
// only if any matching record is found and
//hence it will print `1` only once
//if any matching record is found.
username=rs.getString("rname");
if(get.equals(username))
{
out.println(1);
}
}
Also you are using stmt.executeQuery("select * from newfarmer where rname='"+get+"'");
which is susceptible to SQL injection.
So better use prepared statement instead.
try if(rs.next()),
this will surely help you
I am having a LDAP Queue which process a object class.I cant find the exact location why it is giving the exception. The objclass is a concadenation string with pipe symbol. Any program coding to find the exact location in which concadination part is going to the Exception?.Please Assist.
try {
Attributes objClass = null;
try {
objClass = getObjClass(LdapInfo.PER_ID, person.perId);
} catch (NamingException e)
{
DCXError.myInstance().writeError("LdapUpdaterConnection: " + e.getMessage());
}
NamingEnumeration oc = objClass.get("objectclass").getAll();
String baseObjClass = null;
while (oc.hasMoreElements()) {
baseObjClass = (String) oc.nextElement();
if (baseObjClass.equalsIgnoreCase(LdapInfo.NON_EMPLOYEE_PERSON)
|| baseObjClass.equalsIgnoreCase("N/A")||
baseObjClass.equalsIgnoreCase(LdapInfo.EMPLOYEE_PERSON))
break;
}
} catch (SchemaViolationException e4) {
DCXError.myInstance().writeError(
"LdapUpdaterConnection:doUpdate SchemaViolationException "+ e4.getExplanation());
DCXError.myInstance().writeError("LdapUpdaterConnection:update persID = " + personId);
return (LdapUpdaterConnection.BAD_DATA);
}
You can't find the exact location only because you haven't logged the stack trace. You would also need to reformat your code so that each statement is on a separate line to make any use of that information. You should also use variable names that actually correspond to the content.
This is really terrible code.
It's also hard to see why you are doing all this in the first place. A decent query filter would do all that for you far more simply.