Min forespørgsmel bliver kørt to gange...
Jeg kalder denne bean..import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.*;
/*
parametere til open metoden er:
"org.gjt.mm.mysql.Driver","jdbc:mysql://localhost/DatabaseNavn","username","password"
*/
public class dbHandler extends Object
{
private Connection con=null;
/** Creates new DataBase */
public dbHandler()
{
}
public boolean open(String dbDriverName, String dbClassName, String user, String pass)
{
try
{
if(con==null)
{
Class.forName(dbDriverName).newInstance();
con = DriverManager.getConnection(dbClassName, user, pass);
System.out.println("con = "+con);
}
}
catch (SQLException e)
{
e.printStackTrace(System.out);
return false;
}
catch (ClassNotFoundException e)
{
e.printStackTrace(System.out);
return false;
}
catch (InstantiationException e)
{
e.printStackTrace(System.out);
return false;
}
catch (IllegalAccessException e)
{
e.printStackTrace(System.out);
return false;
}
return true;
}
public void close()
{
if(con!=null)
{
try
{
con.close();
con=null;
}
catch (SQLException e)
{
e.printStackTrace(System.out);
}
}
}
public int doUpdate(String sql)
{
if(con!=null)
{
try
{
long time=System.currentTimeMillis();
Statement stat=con.createStatement();
System.out.println("SQL Update = "+(System.currentTimeMillis()-time)+" ms "+sql);
return stat.executeUpdate(sql);
}
catch (SQLException e)
{
e.printStackTrace(System.out);
}
}
return 0;
}
public ResultSet doQuery(String sql)
{
if(con!=null)
{
try
{
long time=System.currentTimeMillis();
Statement stat=con.createStatement();
System.out.println("SQL Update = "+(System.currentTimeMillis()-time)+" ms "+sql);
return stat.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace(System.out);
}
}
return null;
}
}
Med den JSP kode.
<%@ page import='java.sql.*, javax.sql.*, javax.naming.*' %>
<%@ page language=javascript %>
<% var title='Counter' %>
<html>
<head>
<title><%= title %></title>
</head>
<body bgcolor='white'>
<jsp:useBean id='dbhandler' class='dbHandler' scope='application'/>
<h1><%= title %></h1>
<% dbhandler.open("org.gjt.mm.mysql.Driver","jdbc:mysql://localhost/kodebank","root","");
dbhandler.doUpdate("Create table faq(id int not null, question varchar(100) not null, anser varchar(222) null)");
dbhandler.close();
%>
<% out.println("hejsa"); %>!
</body></html>
og får denne fejl.
SQL Update = 0 ms Create table faq(id int not null, question varchar(100) not n
ull, anser varchar(222) null)
con = org.gjt.mm.mysql.jdbc2.Connection@19762f
SQL Update = 0 ms Create table faq(id int not null, question varchar(100) not n
ull, anser varchar(222) null)
java.sql.SQLException: General error: Table 'faq' already exists
Selv om jeg kun kører jsp siden en gang.... nogen forklaringer?