I am having problem in connecting to mysql database when I am using this code.I have already checked that port number is 3128.So there is no issue about that.I checked it and I think problem is in
connection= DriverManager.getConnection("jdbc:mysql://localhost:3128/gcc","root", "root");
There is no error when compiling.Can someone help me with this?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.sql.*;
//import java.net.*;
public class Main extends Applet implements ActionListener
{
TextArea tarea;
Button bsubmit;
public void init()
{
setBackground(new Color(0,0,0));
setForeground(Color.white);
Label l1=new Label("Write your code : ");
l1.setFont(new Font("lucida console",Font.PLAIN,25));
l1.setSize(200,30);
tarea=new TextArea();
tarea.setFont(new Font("lucida console",Font.PLAIN,18));
tarea.setForeground(new Color(0,0,0));
tarea.setSize(600,250);
bsubmit=new Button("Submit");
bsubmit.setFont(new Font("lucida console",Font.PLAIN,15));
bsubmit.setBackground(new Color(255,255,255));
bsubmit.setForeground(new Color(0,0,0));
bsubmit.setSize(100,30);
add(l1);
add(tarea);
add(bsubmit);
setLayout(null);
l1.setLocation(40,40);
tarea.setLocation(40,100);
bsubmit.setLocation(40,400);
bsubmit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==bsubmit)
{
Connection connection=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost:3128/gcc","root", "root");
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE test2 (code VARCHAR(254))");
}
catch (Exception e) {}
}
}
}
Java database drivers are the worst thing. I remember, I had my first JDBC driver (or let’s say: a working transaction;) to be installed within “only” 2 days, and I know a guy who had two weeks, lol.
In most of the time the very last issue is a SQL problem. Because you need to give the rights to the user as “username”@”IP”. You find the user information in the database information_schema in the table user_privileges. There exist enough tutorials how to handle them, but I like ‘username’@’%’ to get all privileges in a test environment. smile
This may help you: http://dev.mysql.com/doc/refman/5.1/de/adding-users.html
Yeah, those damn SQL rights… ;D
@Luiggi: That’s certainly no silly question! I had hours to find that out!! I mean, which beginner knows that the Java sql classes are only xxxxxxx interfaces with no implemented code?! %D
EDIT
And anyway:
Does this code line look like Java? Nooo, its a xxxxxx compiler hack or whatever! But it works, it really DOES something!!
Oh, but for certain driver implementations this won’t work, you need this:
LMAO!
@Abhinav: Add this line above your getConnection-line:
And don’t think one second about what it exactly does (I know it, but…let’s screw it 😉