📄 mysql.java
字号:
package doktorucuz;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
public class Mysql
{
public static final int OK = 0;
public static final int ERROR = 1;
public static final int DUPLICATE = 2;
private static final String URL = "jdbc:mysql://localhost:3306/doktorucuz?user=root&password=1234";
public Statement s_Query;
public ResultSet m_ResultSet;
public String strSql;
private Connection connection;
public int connectMysql()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(URL);
s_Query = connection.createStatement();
}
catch(Exception e)
{
e.printStackTrace() ;
return ERROR;
}
return OK;
}
public void disconnectMysql()
{
try
{
connection.close();
}
catch(Exception e)
{
e.printStackTrace() ;
}
}
public int insert()
{
try
{
s_Query.executeUpdate(strSql);
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
if (e.getErrorCode() == 1062)
return DUPLICATE;
return ERROR;
}
catch(Exception e)
{
e.printStackTrace() ;
return ERROR;
}
return OK;
}
public int select()
{
try
{
m_ResultSet = s_Query.executeQuery(strSql);
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
return ERROR;
}
catch(Exception e)
{
e.printStackTrace() ;
return ERROR;
}
return OK;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -