Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Notes for others before you try this You must grant all permission to root as follows otherwise this will not work
- At command prompt to enter MySQL type the following mysql -u root -p
- This will request password from you and if successful you should see the following
- as prompt
- MariaDB [(none)]>
- At this prompt type the following below
- GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your password here'; ## now hit enter key
- You should have returned this below or similar
- Query OK, 1 rows affected (0.03 sec)
- Now at the same prompt type like so then hit the enter key
- MariaDB [(none)]> quit
- MD Harrington 23:42Hrs Saturday 8th February 2020
- */
- package mariadb;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class SimpleJDBCApplication {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
- static final String DB_URL = "jdbc:mariadb://localhost/Business";
- // Database credentials
- final String USER = "root";
- final String PASS = "xxxxx"; // Dont forget to put your password in here
- private Statement stmt = null ;
- private ResultSet rs ;
- private Properties props ;
- public SimpleJDBCApplication() {
- props = new Properties();
- props.put(this.USER,USER);
- props.put(this.PASS,PASS) ;
- }
- public Connection getConnection()
- {
- Connection conn = null ;
- System.out.println("Connecting to a selected database...");
- try {
- conn = DriverManager.getConnection(
- DB_URL, props.getProperty(USER), props.getProperty(PASS));
- } catch (SQLException ex) {
- Logger.getLogger(SimpleJDBCApplication.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("Connected database successfully...");
- return conn ;
- }
- public void executesql(Connection con , String sqlcommand){
- try {
- stmt = con.createStatement();
- stmt.executeUpdate(sqlcommand);
- System.out.println("Sql Success in given database...");
- } catch (SQLException ex) {
- Logger.getLogger(SimpleJDBCApplication.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- //finally block used to close resources
- try {
- if (stmt != null) {
- stmt.close();
- }
- } catch (SQLException se) {
- }// do nothing
- try {
- if (con != null) {
- con.close();
- }
- } catch (SQLException se) {
- // deal with sql exceptions here
- }//end finally try
- }//end try
- }
- public void retrieveData(Connection con , String sql)
- {
- try {
- stmt = con.createStatement();
- rs = stmt.executeQuery(sql);
- // how to get the Max column count in your table
- ResultSetMetaData rsmd = rs.getMetaData();
- // this returns number of coloumbs in your table you created
- int numCols = rsmd.getColumnCount();
- /*Print the data to the console rs RS or your record set contains the number of columns for matching
- records */
- while(rs.next()){
- /* Iterate through each column */
- /* Since I know that my table uses only VARCHAR type I need only use rs,GetString() method */
- for(int i= 1 ; i <=numCols ; i++ )
- {
- System.out.print("|" rs.getString(i) + "|" "\t" );
- }
- System.out.println(""); // add a line after printing this in tabbed format
- }
- } catch (SQLException ex) {
- Logger.getLogger(SimpleJDBCApplication.class.getName()).log(Level.SEVERE, null, ex);
- }finally {
- //finally block used to close resources
- try {
- if (stmt != null) {
- stmt.close();
- }
- } catch (SQLException se) {
- }// do nothing
- try {
- if (con != null) {
- con.close(); // always close the connection
- }
- } catch (SQLException se) {
- // deal with sql exceptions here
- }
- } //end finally try
- }
- public static void main(String[] args) {
- SimpleJDBCApplication myApp ; // set aside your memory location for this object
- String sql ; // for holding your sql commands
- myApp = new SimpleJDBCApplication() ; // create a new instance
- // create table
- sql = "CREATE TABLE IF NOT EXISTS CONTACTS" +
- "(" +
- " ClientId VARCHAR(30) not NULL, " +
- " First_name VARCHAR(30), " +
- " Last_Name VARCHAR(30), " +
- " Telephone VARCHAR(30), " +
- " Mobile VARCHAR(30), " +
- " Email VARCHAR(40), " +
- " PostCode VARCHAR(50), " +
- " PRIMARY KEY (ClientId)" +
- ")";
- myApp.executesql(myApp.getConnection(), sql); // invoke the method calls on your my App object
- // this will only run once due to using unique key You cannot have tow or more records with same ID
- sql = "INSERT INTO Business.CONTACTS " +
- "(ClientId, First_name, Last_Name, Telephone, Mobile, Email, PostCode) "+
- "VALUES ('HAR1234', 'Mark', 'Harrington', '0208 8218564', '07234 123123', 'someone@hotmail.com', 'BR3 3PZ')" ;
- myApp.executesql(myApp.getConnection(), sql);
- // check to see if your table was populated with above data
- sql = "SELECT * From Business.CONTACTS " ;
- myApp.retrieveData(myApp.getConnection(), sql);
- System.out.println("Goodbye! \n From Main Method \n Now you can start creating a GUI to do this for you");
- }//end main
- }//end JDBCExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement