Java moment
This commit is contained in:
21
Semester 3/Assignments/DBConnectionTest/pom.xml
Normal file
21
Semester 3/Assignments/DBConnectionTest/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>edu.slcc.asdv.caleb</groupId>
|
||||
<artifactId>DBConnectionTest</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.33</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<exec.mainClass>edu.slcc.asdv.caleb.dbconnectiontest.DBConnectionTest</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.caleb.dbconnectiontest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class DBConnectionTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,129 @@
|
||||
package edu.slcc.asdv.caleb.dbconnectiontest;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author a. v. markou
|
||||
*/
|
||||
public class Database
|
||||
{
|
||||
|
||||
static String result = "";
|
||||
|
||||
private static Connection connection() //throws InstantiationException, IllegalAccessException
|
||||
{
|
||||
|
||||
String databaseName = "suppliers_parts_23";
|
||||
String userName = "admin";
|
||||
String password = "RangerDog01!";
|
||||
String URL2 = "com.mysql.jdbc.Driver";
|
||||
//String URL2 = "com.mysql.cj.jdbc.Driver";
|
||||
Connection con = null;
|
||||
try
|
||||
{// Load Sun's jdbc driver
|
||||
Class.forName(URL2).newInstance();
|
||||
System.out.println("JDBC Driver loaded!");
|
||||
}
|
||||
catch (Exception e) // driver not found
|
||||
{
|
||||
System.err.println("Unable to load database driver");
|
||||
System.err.println("Details : " + e);
|
||||
return null;
|
||||
}
|
||||
String ip = "localhost"; //internet connection
|
||||
String url = "jdbc:mysql://" + ip + ":3306/" + databaseName;
|
||||
try
|
||||
{
|
||||
con = DriverManager.getConnection(url, userName, password);
|
||||
con.setReadOnly(false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println(e.toString());
|
||||
return null;
|
||||
}
|
||||
System.out.println("connection successfull");
|
||||
return con;
|
||||
}
|
||||
|
||||
public String getResult()
|
||||
{
|
||||
return "<p style=\"color:green\">Suppliers <br />" + result;
|
||||
|
||||
}
|
||||
|
||||
public static void closeDatabaseConnection( Connection con)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (con != null)
|
||||
{
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
result = e.toString();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static void listAllSuppliers()
|
||||
{
|
||||
Connection con = connection();
|
||||
if (con == null)
|
||||
{
|
||||
result = "cannot connect to database" ;
|
||||
return ;
|
||||
}
|
||||
String table = "";
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
String sqlStr = "SELECT * FROM supplier";
|
||||
try
|
||||
{
|
||||
//prepare statement
|
||||
ps = con.prepareStatement(sqlStr);
|
||||
//execute
|
||||
rs = ps.executeQuery();
|
||||
System.out.println("before entering while");
|
||||
while (rs.next())
|
||||
{
|
||||
//System.out.println("inside while");
|
||||
String sNumber = rs.getString(1) + " ";
|
||||
String sName = rs.getString(2) + " ";
|
||||
String status = rs.getDate(3) + " ";
|
||||
String city = rs.getString(4) + " ";
|
||||
table += sNumber + sName + status + city + "\n";
|
||||
}
|
||||
System.out.println(table);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
closeDatabaseConnection( con);
|
||||
// close the resources
|
||||
if (ps != null)
|
||||
{
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
catch (SQLException sqle){ sqle.printStackTrace(); }
|
||||
}
|
||||
result = table;
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
listAllSuppliers();
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.dbconnectiontest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Scroller {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
String string = "Java moment";
|
||||
while (true) {
|
||||
System.out.println(string);
|
||||
System.out.print("\033[H\033[2J");
|
||||
System.out.flush();
|
||||
string = string.charAt(string.length() - 1) + string.substring(0, string.length() - 1);
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user