MySQL path set in ubuntu:
https://help.ubuntu.com/community/JDBCAndMySQL
First, run this 3 commands:
https://help.ubuntu.com/community/JDBCAndMySQL
First, run this 3 commands:
$ sudo apt-get install mysql-server $ sudo apt-get install mysql-client $ sudo apt-get install libmysql-java
Then , set up MySQL default password:
$ mysqladmin -u root password root
$ mysql -u root -p
After that, Create SQL database and tables
mysql> create database emotherearth;
Setting up the user to use JDBC
you can set it for all users, by editing /etc/environment
(use sudo command - $ sudo gedit /etc/environment)
CLASSPATH=".:/usr/share/java/mysql.jar"
Log
out and Log in again. Start up a terminal and type:
$ echo $CLASSPATH
It should print out something like ":/usr/share/java/mysql.jar"
Testing a java program.
It should now work.
import java.sql.*; import java.util.Properties; public class DBTestDemo { // The JDBC Connector Class. private static final String dbClassName = "com.mysql.jdbc.Driver"; // Connection string. macanova is the database the program // is connecting to. You can include user and password after this // by adding (say) ?user=rizvi&password=rizvi123. Not recommended! private static final String CONNECTION = "jdbc:mysql://127.0.0.1/macanova"; public static void main(String[] args) throws ClassNotFoundException,SQLException { System.out.println(dbClassName); // Class.forName(xxx) loads the jdbc classes and // creates a drivermanager class factory Class.forName(dbClassName); // Properties for user and password. Here the user rizvi and password rizvi123 Properties p = new Properties(); p.put("user","rizvi"); p.put("password","rizvi123"); // Now try to connect Connection c = DriverManager.getConnection(CONNECTION,p); System.out.println("It works !"); c.close(); } }
No comments:
Post a Comment