Jump to content
Sign in to follow this  
programmer1337

[Resolvido] JDBC

Recommended Posts

programmer1337

Boas pessoal!! O problema é o seguinte, não consigo fazer com que a query seja executada, ou seja a ligação á base de dados é feita e consigo listar o ke existe nela mas quando tento inserir nao insere :S alguem me pode ajudar? Cumprimentos

public String registaCompanhia(String nome, int tipo_companhia, String web, String facebook, String email, int telefone, String latitude, String	longitude) throws ClassNotFoundException{

	Connection con = null;
	String url = "jdbc:mysql://localhost:3306/";
	String db = "companhia";
	String driver = "com.mysql.jdbc.Driver";

	try{
		Class.forName(driver);
		con = DriverManager.getConnection(url+db,"root","");
		Statement st = con.createStatement();


		String sql="INSERT INTO companhia (nome, tipo_companhia, web, facebook, email, telefone, latitude, longitude) VALUES ('"+nome+"','" +tipo_companhia+"', '"+web+"', '"+facebook+"', '"+email+"', '"+telefone+"', '"+latitude+"', '"+longitude+"');";

		st.executeUpdate(sql);

		st.close();
		con.close();

		}
		catch (SQLException s){
			System.out.println("SQL statement is not executed!");
		}

	return "Companhia Registada com Sucesso!";

}

Share this post


Link to post
Share on other sites
programmer1337

Não :/ não da erro nenhum e não lança nenhuma excepção e é isso que acho mais estranho :S

Share this post


Link to post
Share on other sites
Baderous

Em vez de teres aquele print dentro do catch, mete isto:

s.printStackTrace();

E vê se é mostrada alguma informação na consola.

Só para ter a certeza de que não há algo de errado.

Share this post


Link to post
Share on other sites
programmer1337

Pois tinhas razão assim que adicionei aquilo apareceu-me isto :X

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`companhia`.`companhia`, CONSTRAINT `companhia_ibfk_1` FOREIGN KEY (`tipo_companhia`) REFERENCES `tipo_companhia` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

at com.mysql.jdbc.Util.getInstance(Util.java:386)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)

at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1698)

at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1617)

at RMIImpl.registaCompanhia(RMIImpl.java:95)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)

at sun.rmi.transport.Transport$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.Transport.serviceCall(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Share this post


Link to post
Share on other sites
Baderous

O problema está ao nível da BD, provavelmente estás a inserir um tipo_companhia na tabela companhia que ainda não existe na tabela tipo_companhia.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.