-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/zukahai/Java-code
- Loading branch information
Showing
4 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package Test; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.EventQueue; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JPanel; | ||
import javax.swing.border.EmptyBorder; | ||
|
||
import com.mysql.cj.xdevapi.PreparableStatement; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
import javax.swing.JTextField; | ||
import javax.swing.JLabel; | ||
import javax.swing.JButton; | ||
import java.awt.event.ActionListener; | ||
import java.awt.event.ActionEvent; | ||
|
||
public class AddStudent extends JFrame { | ||
|
||
Connect c = new Connect(); | ||
Connection conn = c.newConnection(); | ||
|
||
private JPanel contentPane; | ||
private JTextField masv; | ||
private JTextField name; | ||
private JTextField age; | ||
|
||
/** | ||
* Launch the application. | ||
*/ | ||
public static void main(String[] args) { | ||
EventQueue.invokeLater(new Runnable() { | ||
public void run() { | ||
try { | ||
AddStudent frame = new AddStudent(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Create the frame. | ||
*/ | ||
public AddStudent() { | ||
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
setVisible(true); | ||
setBounds(100, 100, 450, 300); | ||
contentPane = new JPanel(); | ||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||
setContentPane(contentPane); | ||
contentPane.setLayout(null); | ||
|
||
masv = new JTextField(); | ||
masv.setBounds(129, 35, 282, 34); | ||
contentPane.add(masv); | ||
masv.setColumns(10); | ||
|
||
JLabel lblNewLabel = new JLabel("MaSV"); | ||
lblNewLabel.setBounds(39, 45, 80, 14); | ||
contentPane.add(lblNewLabel); | ||
|
||
JLabel lblName = new JLabel("Name"); | ||
lblName.setBounds(39, 100, 80, 14); | ||
contentPane.add(lblName); | ||
|
||
name = new JTextField(); | ||
name.setColumns(10); | ||
name.setBounds(129, 90, 282, 34); | ||
contentPane.add(name); | ||
|
||
JLabel lblAge = new JLabel("Age"); | ||
lblAge.setBounds(39, 156, 80, 14); | ||
contentPane.add(lblAge); | ||
|
||
age = new JTextField(); | ||
age.setColumns(10); | ||
age.setBounds(129, 146, 282, 34); | ||
contentPane.add(age); | ||
|
||
JButton add = new JButton("Add Student"); | ||
add.addActionListener(new ActionListener() { | ||
public void actionPerformed(ActionEvent e) { | ||
try { | ||
String sql = "INSERT INTO student VALUES (?, ?, ?)"; | ||
PreparedStatement stm = conn.prepareStatement(sql); | ||
stm.setString(1, masv.getText()); | ||
stm.setString(2, name.getText()); | ||
stm.setInt(3, Integer.parseInt(age.getText())); | ||
stm.execute(); | ||
System.out.println("Da them"); | ||
|
||
// String sql = "DELETE FROM student"; | ||
// PreparedStatement stm = conn.prepareStatement(sql); | ||
// stm.execute(); | ||
} catch (SQLException e1) { | ||
// TODO Auto-generated catch block | ||
e1.printStackTrace(); | ||
} | ||
} | ||
}); | ||
add.setBounds(129, 207, 189, 23); | ||
contentPane.add(add); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package UI; | ||
|
||
import java.awt.BorderLayout; | ||
import java.awt.EventQueue; | ||
|
||
import javax.swing.JFrame; | ||
import javax.swing.JPanel; | ||
import javax.swing.border.EmptyBorder; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Vector; | ||
|
||
import connectsql.Connect; | ||
|
||
import javax.swing.JButton; | ||
import java.awt.event.ActionListener; | ||
import java.awt.event.ActionEvent; | ||
import javax.swing.JTable; | ||
import javax.swing.JScrollPane; | ||
import javax.swing.table.DefaultTableModel; | ||
|
||
public class ShowTable extends JFrame { | ||
|
||
private JPanel contentPane; | ||
private JTable table; | ||
private Vector vT, vD; | ||
|
||
/** | ||
* Launch the application. | ||
*/ | ||
public static void main(String[] args) { | ||
EventQueue.invokeLater(new Runnable() { | ||
public void run() { | ||
try { | ||
ShowTable frame = new ShowTable(); | ||
frame.setVisible(true); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Create the frame. | ||
*/ | ||
public ShowTable() { | ||
Connect cn = new Connect(); | ||
Connection conn = cn.getConnection(); | ||
|
||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
setBounds(100, 100, 780, 511); | ||
contentPane = new JPanel(); | ||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | ||
setContentPane(contentPane); | ||
contentPane.setLayout(null); | ||
|
||
JScrollPane scrollPane = new JScrollPane(); | ||
scrollPane.setBounds(41, 24, 687, 346); | ||
contentPane.add(scrollPane); | ||
|
||
table = new JTable(); | ||
vD = new Vector(); | ||
String sql = "SELECT * FROM table21da"; | ||
PreparedStatement stm; | ||
try { | ||
stm = conn.prepareStatement(sql); | ||
ResultSet rs = stm.executeQuery(); | ||
while(rs.next()) { | ||
// System.out.println(rs.getInt(1) + "\t" + rs.getString(2)); | ||
Vector t = new Vector(); | ||
t.add(rs.getInt(1)); | ||
t.add(rs.getString(2)); | ||
vD.add(t); | ||
} | ||
} catch (SQLException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
|
||
vT = new Vector(); | ||
vT.add("ID"); | ||
vT.add("Name"); | ||
table.setModel(new DefaultTableModel(vD, vT)); | ||
scrollPane.setViewportView(table); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package connectsql; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
|
||
|
||
public class Connect { | ||
|
||
public Connection getConnection() { | ||
Connection conn = null; | ||
try{ | ||
String userName = "root"; | ||
String password = ""; | ||
String DatabaseName = "test"; | ||
String url = "jdbc:mysql://localhost/" + DatabaseName; | ||
Class.forName ("com.mysql.jdbc.Driver"); | ||
conn = DriverManager.getConnection(url, userName, password); | ||
System.out.println("Ok"); | ||
} catch (Exception e) { | ||
// TODO: handle exception | ||
e.printStackTrace(); | ||
} | ||
return conn; | ||
} | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
Connection conn = null; | ||
try{ | ||
//cmt | ||
String userName = "root"; | ||
String password = ""; | ||
String DatabaseName = "test"; | ||
String url = "jdbc:mysql://localhost/" + DatabaseName; | ||
Class.forName ("com.mysql.jdbc.Driver"); | ||
conn = DriverManager.getConnection(url, userName, password); | ||
System.out.println("Ok"); | ||
|
||
|
||
// String sql = "SELECT * FROM table21da"; | ||
// PreparedStatement stm = conn.prepareStatement(sql); | ||
// ResultSet rs = stm.executeQuery(); | ||
// while(rs.next()) { | ||
// System.out.println(rs.getInt(1) + "\t" + rs.getString(2)); | ||
// } | ||
// | ||
// | ||
// | ||
// sql = "INSERT INTO table21da(ID, Name) VALUES (913, 'SJDFJDSJFSDJ')"; | ||
// stm = conn.prepareStatement(sql); | ||
// stm.execute(); | ||
// | ||
// sql = "INSERT INTO table21da(ID, Name) VALUES (897, 'alophuday')"; | ||
// stm = conn.prepareStatement(sql); | ||
// stm.execute(); | ||
|
||
} catch (Exception e) { | ||
// TODO: handle exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters