A simple Javamysql database system

Source: Internet
Author: User

Package databasetest;

Import Java.io.FileInputStream;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import java.util.Properties;
Import Java.util.Scanner;

Public class Functiontodatabase {

Private String driver;
Private String URL;
Private String user;
Private String Pass;  

public void Initparam (String paramfile) throws exception{

Properties props = new Properties ();
Props.load (New FileInputStream (Paramfile));
Driver = props.getproperty ("Driver");
URL = props.getproperty ("url");
user = Props.getproperty ("user");
Pass = props.getproperty ("Pass");
}

public void work () throws exception{
Class.forName (driver);
}  

public void set (String sql) throws exception{
This.work ();
  Try (
Connection conn = drivermanager.getconnection (url,user,pass);
  Statement stmt = conn.createstatement ())
{
Stmt.execute (SQL);
}
}

public void Switchfunction () throws exception{

Scanner sc = new Scanner (system.in);

System.out.println ("Please input your select 1-5:");
System.out.println ("************************************");
System.out.println ("1---add");
System.out.println ("2---delete");
System.out.println ("3---alter");
System.out.println ("4---search");
System.out.println ("5---exit System");
System.out.println ("************************************");
System.out.println ("now! Please select your choice to manage the database! ");

Switch (Sc.nextint ()) {
Case 1://add
Add ();
Switchfunction ();
Break
Case 2://delete
Delete ();
Switchfunction ();
Break
Case 3://alter
Alter ();
Switchfunction ();
Break
Case 4://search
Search ();
Switchfunction ();
Break
Case 5://exit
System.exit (0);
Break
Default
SYSTEM.OUT.PRINTLN ("-------input error,try it again! -------");
Switchfunction ();
Break
}
}

Add data to Database
public void Add () {
Scanner sc = new Scanner (system.in);

try{

This.work ();
Connection conn = drivermanager.getconnection (Url,user,pass);
PreparedStatement pstmt = conn.preparestatement ("INSERT into" +
"Connecttest values (?,?,?,?);");

Name Age Number Money

SYSTEM.OUT.PRINTLN ("Please input your times your want to insert:");
int cout = Sc.nextint ();
SYSTEM.OUT.PRINTLN ("Please input the data:");
for (int j = 0; J < cout; J + +) {
System.out.println ("name" + (j+1) + ":");
String tname = Sc.next ();
System.out.println ("Age" + (j+1) + ":");
int tage = Sc.nextint ();
System.out.println ("number" + (j+1) + ":");
int tnum = Sc.nextint ();
System.out.println ("Money" + (j+1) + ":");
Double Tmoney = sc.nextdouble ();
Pstmt.setstring (1,tname);
Pstmt.setint (2,tage);
Pstmt.setint (3,tnum);
Pstmt.setdouble (4,tmoney);

Pstmt.executeupdate ();
}
}
catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Insert sucess!");

}

Delete data in database
Delete from
public void Delete () throws exception{
Scanner sc = new Scanner (system.in);
This.work ();
Try
Connection conn = drivermanager.getconnection (Url,user,pass);
Statement stmt = Conn.createstatement ();
PreparedStatement pstmt = conn.preparestatement ("Delete from Connecttest"
+ "where number =?;");
){
SYSTEM.OUT.PRINTLN ("Please input the number of want to delete:");
int num = Sc.nextint ();
Pstmt.setint (1, NUM);
Pstmt.executeupdate ();
System.out.println ("delete sucess!");
}
}

Alter data in database
Modify Money
public void Alter () throws exception{
Scanner sc = new Scanner (system.in);
This.work ();
Try
Connection conn = drivermanager.getconnection (Url,user,pass);
PreparedStatement pstmt = conn.preparestatement ("Update Connecttest" +
"Set money =?" +
"Where number =?;");
){
SYSTEM.OUT.PRINTLN ("Please input the number of you want to alter:");
Thread.Sleep (1000);
int tnum = Sc.nextint ();
SYSTEM.OUT.PRINTLN ("Please input your money:");
Double Tmoney = sc.nextdouble ();
Pstmt.setdouble (1, Tmoney);
Pstmt.setint (2, Tnum);
Pstmt.executeupdate ();
SYSTEM.OUT.PRINTLN ("Alter sucess!");
}

}

Find the data by index
public void Search () throws exception{
Scanner sc = new Scanner (system.in);
This.work ();
Try
Connection conn = drivermanager.getconnection (Url,user,pass);
PreparedStatement pstmt = conn.preparestatement ("Select from Cnnnecttest" +
"Where number =?;");
ResultSet rs = Pstmt.executequery ();
){
SYSTEM.OUT.PRINTLN ("Please input the number of want to search:");
int tnum = Sc.nextint ();
Pstmt.setint (1,tnum);

System.out.println (rs.getstring (1) + ' t ' + rs.getstring (2));

}
System.out.println ("Search sucess!");
}

public void Drop (String sql) throws exception{
This.work ();
Try
Connection conn = drivermanager.getconnection (Url,user,pass);
Statement stmt = Conn.createstatement ()
)
{
Stmt.executeupdate (SQL);
}
}

public static void Main (string[] args) throws Exception {

Functiontodatabase FTD = new Functiontodatabase ();
Ftd.initparam ("Mysql.ini");
//?? Ô?? Create Databaseÿÿÿ
Ftd.drop ("drop Database Databasetest");
Ftd.set ("CREATE Database databasetest;");
Ftd.drop ("drop table if exists connecttest;");
Ftd.set ("CREATE table Connecttest (name varchar (255) NOT NULL,"
+ "Age int not NULL," + "number int (one) primary key,"
+ "Money double is not null);");

System.out.println ("welcom!");
Ftd.switchfunction ();

Ftd.drop ("drop table connecttest;");
Ftd.drop ("drop Database Databasetest");
}
}

A simple Javamysql database system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.