hello guys... just wanna ask.. how to connect in mySQL?
thanks!

Recommended Answers

All 4 Replies

Read the examples that come with the documentation that comes with the driver that you download.

public Connection getConnection() {
        Connection conn=null;
        // Loading driver
        try {
            String url = "jdbc:odbc:headnote";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );
            conn = DriverManager.getConnection( url );
        } catch ( ClassNotFoundException cnfex ) {
            cnfex.printStackTrace();
        } catch ( SQLException sqlex ) {
            sqlex.printStackTrace();
        } catch ( Exception excp ) {
            excp.printStackTrace();
        }
        
        return conn;
        
    }
    void write_data(String s,String no) {
        try {
            String caseno=no.substring(no.lastIndexOf("\\")+1,no.length()-4);
            Connection c=getConnection();
            s=s.replace('\'', ' ');
            System.out.println(s);
            Statement stClients = c.createStatement();
            String query="insert into hn1 values "+"( "+" \'" +caseno+"\'"+","+"\'"+s+"\'"+")";
            int rsClients= stClients.executeUpdate(query);
            System.out.println("ok ");
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

This one is code for jdbc.
U have to include java.sql package for it.
It is working well.
I hope that it will help U.
Bye tc.

manjusaharan uses a connection through odbc.
Which is not the best choice when connecting to a MySQL database.

You should download the JConnector of mySql
http://www.mysql.com/products/connector/j/

Then an exemple code to connect to the database:

String dbdriver = "com.mysql.jdbc.Driver";
private String connectionstring = "jdbc:mysql://localhost:3306/databasename?user=root&password=yourpwd";
	
Class.forName(dbdriver).newInstance();
cnn = DriverManager.getConnection(connectionstring);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.